blob: a70743875d276dc00e46647822f223a5c140bf1a [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>
Neels Hofmeyra160e4b2018-11-16 00:16:57 +010045#include <osmocom/core/endian.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020046#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020047
48#define NUM_E1_TS 32
49
50static void *tall_e1inp_ctx;
51
52/* list of all E1 drivers */
53LLIST_HEAD(e1inp_driver_list);
54
55/* list of all E1 lines */
56LLIST_HEAD(e1inp_line_list);
57
58static void *tall_sigl_ctx;
59
Harald Weltef2737fc2011-08-16 14:30:10 +020060static const struct rate_ctr_desc e1inp_ctr_d[] = {
61 [E1I_CTR_HDLC_ABORT] = {
Pau Espin Pedrola2106842018-07-24 15:00:11 +020062 "hdlc:abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020063 },
64 [E1I_CTR_HDLC_BADFCS] = {
Pau Espin Pedrola2106842018-07-24 15:00:11 +020065 "hdlc:bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020066 },
67 [E1I_CTR_HDLC_OVERR] = {
Pau Espin Pedrola2106842018-07-24 15:00:11 +020068 "hdlc:overrun", "HDLC Overrun"
Harald Weltef2737fc2011-08-16 14:30:10 +020069 },
70 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020071 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020072 },
73 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020074 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020075 },
76};
77
78static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
79 .group_name_prefix = "e1inp",
80 .group_description = "E1 Input subsystem",
81 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
82 .ctr_desc = e1inp_ctr_d,
83};
84
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020085/*
86 * pcap writing of the misdn load
87 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
88 */
89#define DLT_LINUX_LAPD 177
90#define PCAP_INPUT 0
91#define PCAP_OUTPUT 1
92
93struct pcap_hdr {
94 uint32_t magic_number;
95 uint16_t version_major;
96 uint16_t version_minor;
97 int32_t thiszone;
98 uint32_t sigfigs;
99 uint32_t snaplen;
100 uint32_t network;
101} __attribute__((packed));
102
103struct pcaprec_hdr {
104 uint32_t ts_sec;
105 uint32_t ts_usec;
106 uint32_t incl_len;
107 uint32_t orig_len;
108} __attribute__((packed));
109
110struct fake_linux_lapd_header {
111 uint16_t pkttype;
112 uint16_t hatype;
113 uint16_t halen;
114 uint64_t addr;
115 int16_t protocol;
116} __attribute__((packed));
117
118struct lapd_header {
Neels Hofmeyra160e4b2018-11-16 00:16:57 +0100119#if OSMO_IS_LITTLE_ENDIAN
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200120 uint8_t ea1 : 1;
121 uint8_t cr : 1;
122 uint8_t sapi : 6;
123 uint8_t ea2 : 1;
124 uint8_t tei : 7;
125 uint8_t control_foo; /* fake UM's ... */
Neels Hofmeyra160e4b2018-11-16 00:16:57 +0100126#elif OSMO_IS_BIG_ENDIAN
127/* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */
128 uint8_t sapi:6, cr:1, ea1:1;
129 uint8_t tei:7, ea2:1;
130 uint8_t control_foo;
131#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200132} __attribute__((packed));
133
134osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
135osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
136osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
137osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
138osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
139
140
141static int pcap_fd = -1;
142
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200143int e1_set_pcap_fd(int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200144{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200145 struct pcap_hdr header = {
146 .magic_number = 0xa1b2c3d4,
147 .version_major = 2,
148 .version_minor = 4,
149 .thiszone = 0,
150 .sigfigs = 0,
151 .snaplen = 65535,
152 .network = DLT_LINUX_LAPD,
153 };
154
155 pcap_fd = fd;
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200156 return write(pcap_fd, &header, sizeof(header));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200157}
158
159/* This currently only works for the D-Channel */
160static void write_pcap_packet(int direction, int sapi, int tei,
161 struct msgb *msg) {
162 if (pcap_fd < 0)
163 return;
164
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200165 time_t cur_time;
166 struct tm *tm;
167
168 struct fake_linux_lapd_header header = {
169 .pkttype = 4,
170 .hatype = 0,
171 .halen = 0,
172 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
173 .protocol = ntohs(48),
174 };
175
176 struct lapd_header lapd_header = {
177 .ea1 = 0,
178 .cr = direction == PCAP_OUTPUT ? 1 : 0,
179 .sapi = sapi & 0x3F,
180 .ea2 = 1,
181 .tei = tei & 0x7F,
182 .control_foo = 0x03 /* UI */,
183 };
184
185 struct pcaprec_hdr payload_header = {
186 .ts_sec = 0,
187 .ts_usec = 0,
188 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
189 + sizeof(struct lapd_header),
190 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
191 + sizeof(struct lapd_header),
192 };
193
194
195 cur_time = time(NULL);
196 tm = localtime(&cur_time);
197 payload_header.ts_sec = mktime(tm);
198
Harald Weltec9295ea2014-08-21 02:41:41 +0200199 write(pcap_fd, &payload_header, sizeof(payload_header));
200 write(pcap_fd, &header, sizeof(header));
201 write(pcap_fd, &lapd_header, sizeof(lapd_header));
202 write(pcap_fd, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200203}
204
Harald Welte4ca5c532016-07-27 23:05:03 +0200205const struct value_string e1inp_sign_type_names[5] = {
206 { E1INP_SIGN_NONE, "None" },
207 { E1INP_SIGN_OML, "OML" },
208 { E1INP_SIGN_RSL, "RSL" },
209 { E1INP_SIGN_OSMO, "OSMO" },
210 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200211};
Harald Welte4ca5c532016-07-27 23:05:03 +0200212
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200213const char *e1inp_signtype_name(enum e1inp_sign_type tp)
214{
Harald Welte4ca5c532016-07-27 23:05:03 +0200215 return get_value_string(e1inp_sign_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200216}
217
Harald Welte7a228eb2016-07-28 11:09:31 +0200218const struct value_string e1inp_ts_type_names[6] = {
Harald Welte4ca5c532016-07-27 23:05:03 +0200219 { E1INP_TS_TYPE_NONE, "None" },
220 { E1INP_TS_TYPE_SIGN, "Signalling" },
221 { E1INP_TS_TYPE_TRAU, "TRAU" },
Harald Weltea0108e72016-07-27 21:44:50 +0200222 { E1INP_TS_TYPE_RAW, "RAW" },
Harald Welte7a228eb2016-07-28 11:09:31 +0200223 { E1INP_TS_TYPE_HDLC, "HDLC" },
Harald Welte4ca5c532016-07-27 23:05:03 +0200224 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200225};
226
227const char *e1inp_tstype_name(enum e1inp_ts_type tp)
228{
Harald Welte4ca5c532016-07-27 23:05:03 +0200229 return get_value_string(e1inp_ts_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200230}
231
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200232int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200233{
234 struct e1inp_sign_link *sign_link = msg->dst;
235 struct e1inp_driver *e1inp_driver;
236 struct e1inp_ts *e1i_ts;
Alexander Couzens35daa672016-11-08 16:17:20 +0100237
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200238 msg->l2h = msg->data;
239
240 /* don't know how to route this message. */
241 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200242 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200243 osmo_hexdump(msg->data, msg->len));
244 talloc_free(msg);
245 return -EINVAL;
246 }
247 e1i_ts = sign_link->ts;
248 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
249 /* notify the driver we have something to write */
250 e1inp_driver = sign_link->ts->line->driver;
251 e1inp_driver->want_write(e1i_ts);
252 }
253 msgb_enqueue(&sign_link->tx_list, msg);
254
Harald Welte7f9d8512016-07-04 09:59:46 +0200255 /* we only need to write a 'Fake LAPD' packet here, if the
256 * underlying driver hides LAPD from us. If we use the
257 * libosmocore LAPD implementation, it will take care of writing
258 * the _actual_ LAPD packet */
259 if (!e1i_ts->lapd) {
260 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi,
261 sign_link->tei, msg);
262 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200263
264 return 0;
265}
266
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200267int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200268{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200269 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200270}
271
272/* Timeslot */
273int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200274 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
275 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200276{
277 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
278 return 0;
279
280 ts->type = E1INP_TS_TYPE_TRAU;
281 ts->line = line;
282
283 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200284 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200285 ts->trau.demux.data = ts;
286 subch_demux_init(&ts->trau.demux);
287 return 0;
288}
289
290int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
291{
292 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
293 return 0;
294
295 ts->type = E1INP_TS_TYPE_SIGN;
296 ts->line = line;
297
298 if (line && line->driver)
299 ts->sign.delay = line->driver->default_delay;
300 else
301 ts->sign.delay = 100000;
302 INIT_LLIST_HEAD(&ts->sign.sign_links);
303 return 0;
304}
305
Harald Weltea0108e72016-07-27 21:44:50 +0200306int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line,
307 void (*raw_recv_cb)(struct e1inp_ts *ts,
308 struct msgb *msg))
309{
310 if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line)
311 return 0;
312
313 ts->type = E1INP_TS_TYPE_RAW;
314 ts->line = line;
315 ts->raw.recv_cb = raw_recv_cb;
316 INIT_LLIST_HEAD(&ts->raw.tx_queue);
317
318 return 0;
319}
320
Harald Welte7a228eb2016-07-28 11:09:31 +0200321int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line,
322 void (*hdlc_recv_cb)(struct e1inp_ts *ts,
323 struct msgb *msg))
324{
325 if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line)
326 return 0;
327
328 ts->type = E1INP_TS_TYPE_HDLC;
329 ts->line = line;
330 ts->hdlc.recv_cb = hdlc_recv_cb;
331 INIT_LLIST_HEAD(&ts->hdlc.tx_queue);
332
333 return 0;
334}
335
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200336struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200337{
338 struct e1inp_line *e1i_line;
339
340 /* iterate over global list of e1 lines */
341 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
342 if (e1i_line->num == e1_nr)
343 return e1i_line;
344 }
345 return NULL;
346}
347
348struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200349e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200350{
351 struct e1inp_driver *driver;
352 struct e1inp_line *line;
353 int i;
354
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200355 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200356 if (line) {
Harald Welteb5af0992020-01-12 12:59:52 +0100357 LOGPIL(line, DLINP, LOGL_ERROR, "E1 Line %u already exists\n", e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200358 return NULL;
359 }
360
361 driver = e1inp_driver_find(driver_name);
362 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200363 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364 driver_name);
365 return NULL;
366 }
367
368 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
369 if (!line)
370 return NULL;
371
372 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200373 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200374
375 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
Harald Welteff8eed22017-07-12 00:38:25 +0200376 if (!line->rate_ctr) {
Harald Welteb5af0992020-01-12 12:59:52 +0100377 LOGPIL(line, DLINP, LOGL_ERROR, "Cannot allocate counter group\n");
Harald Welteff8eed22017-07-12 00:38:25 +0200378 talloc_free(line);
379 return NULL;
380 }
Harald Weltef2737fc2011-08-16 14:30:10 +0200381
Harald Weltec2889512011-09-13 23:49:04 +0100382 line->num_ts = NUM_E1_TS;
383 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200384 line->ts[i].num = i+1;
385 line->ts[i].line = line;
386 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200387 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200388 llist_add_tail(&line->list, &e1inp_line_list);
389
390 return line;
391}
392
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200393struct e1inp_line *
394e1inp_line_clone(void *ctx, struct e1inp_line *line)
395{
396 struct e1inp_line *clone;
397
398 /* clone virtual E1 line for this new OML link. */
399 clone = talloc_zero(ctx, struct e1inp_line);
400 if (clone == NULL)
401 return NULL;
402
403 memcpy(clone, line, sizeof(struct e1inp_line));
Stefan Sperlingb0162072018-05-22 18:19:00 +0200404
405 if (line->name) {
406 clone->name = talloc_strdup(clone, line->name);
407 OSMO_ASSERT(clone->name);
408 }
409 if (line->sock_path) {
410 clone->sock_path = talloc_strdup(clone, line->sock_path);
411 OSMO_ASSERT(clone->sock_path);
412 }
413
414 /*
415 * Rate counters and driver data are shared between clones. These are pointers
416 * to dynamic memory so we use reference counting to avoid a double-free (see OS#3137).
417 */
418 OSMO_ASSERT(line->rate_ctr);
419 clone->rate_ctr = talloc_reference(clone, line->rate_ctr);
420 if (line->driver_data)
421 clone->driver_data = talloc_reference(clone, line->driver_data);
422
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200423 clone->refcnt = 1;
424 return clone;
425}
426
427void e1inp_line_get(struct e1inp_line *line)
428{
Vadim Yanitskiyb43ce422019-12-02 01:49:43 +0700429 int old_refcnt = line->refcnt++;
430
Harald Welteb5af0992020-01-12 12:59:52 +0100431 LOGPIL(line, DLINP, LOGL_DEBUG, "Line '%s' (%p) reference count get: %d -> %d\n",
Vadim Yanitskiyb43ce422019-12-02 01:49:43 +0700432 line->name, line, old_refcnt, line->refcnt);
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200433}
434
435void e1inp_line_put(struct e1inp_line *line)
436{
Vadim Yanitskiyb43ce422019-12-02 01:49:43 +0700437 int old_refcnt = line->refcnt--;
438
Harald Welteb5af0992020-01-12 12:59:52 +0100439 LOGPIL(line, DLINP, LOGL_DEBUG, "Line '%s' (%p) reference count put: %d -> %d\n",
Vadim Yanitskiyb43ce422019-12-02 01:49:43 +0700440 line->name, line, old_refcnt, line->refcnt);
441
Stefan Sperlingb0162072018-05-22 18:19:00 +0200442 if (line->refcnt == 0) {
443 /* Remove our counter group from libosmocore's global counter
444 * list if we are freeing the last remaining talloc context.
445 * Otherwise we get a use-after-free when libosmocore's timer
446 * ticks again and attempts to update these counters (OS#3011).
447 *
448 * Note that talloc internally counts "secondary" references
449 * _in addition to_ the initial allocation context, so yes,
450 * we must check for *zero* remaining secondary contexts here. */
451 if (talloc_reference_count(line->rate_ctr) == 0) {
452 rate_ctr_group_free(line->rate_ctr);
453 } else {
454 /* We are not freeing the last talloc context.
455 * Instead of calling talloc_free(), unlink this 'line' pointer
456 * which serves as one of several talloc contexts for the rate
457 * counters and driver private state. */
458 talloc_unlink(line, line->rate_ctr);
459 if (line->driver_data)
460 talloc_unlink(line, line->driver_data);
461 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200462 talloc_free(line);
Stefan Sperlingb0162072018-05-22 18:19:00 +0200463 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200464}
465
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200466void
467e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
468{
469 line->ops = ops;
470}
471
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200472#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200473struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200474{
475 struct e1inp_line *line;
476 int i;
477
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200478 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200479 if (line)
480 return line;
481
482 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
483 if (!line)
484 return NULL;
485
486 line->num = e1_nr;
487 for (i = 0; i < NUM_E1_TS; i++) {
488 line->ts[i].num = i+1;
489 line->ts[i].line = line;
490 }
491 llist_add_tail(&line->list, &e1inp_line_list);
492
493 return line;
494}
495#endif
496
497static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
498{
499 struct e1inp_line *e1i_line;
500
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200501 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502 if (!e1i_line)
503 return NULL;
504
505 return &e1i_line->ts[ts_nr-1];
506}
507
508struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
509{
510 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
511
512 if (!e1i_ts)
513 return NULL;
514
515 return &e1i_ts->trau.mux;
516}
517
518/* Signalling Link */
519
520struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
521 uint8_t tei, uint8_t sapi)
522{
523 struct e1inp_sign_link *link;
524
525 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
526 if (link->sapi == sapi && link->tei == tei)
527 return link;
528 }
529
530 return NULL;
531}
532
533/* create a new signalling link in a E1 timeslot */
534
535struct e1inp_sign_link *
536e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
537 struct gsm_bts_trx *trx, uint8_t tei,
538 uint8_t sapi)
539{
540 struct e1inp_sign_link *link;
541
542 if (ts->type != E1INP_TS_TYPE_SIGN)
543 return NULL;
544
545 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
546 if (!link)
547 return NULL;
548
549 link->ts = ts;
550 link->type = type;
551 INIT_LLIST_HEAD(&link->tx_list);
552 link->trx = trx;
553 link->tei = tei;
554 link->sapi = sapi;
555
556 llist_add_tail(&link->list, &ts->sign.sign_links);
557
558 return link;
559}
560
561void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
562{
563 struct msgb *msg;
564
565 llist_del(&link->list);
566 while (!llist_empty(&link->tx_list)) {
567 msg = msgb_dequeue(&link->tx_list);
568 msgb_free(msg);
569 }
570
571 if (link->ts->type == E1INP_TS_TYPE_SIGN)
572 osmo_timer_del(&link->ts->sign.tx_timer);
573
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200574 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200575 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200576
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200577 e1inp_line_put(link->ts->line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200578 talloc_free(link);
579}
580
581/* XXX */
582/* the E1 driver tells us he has received something on a TS */
583int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
584 uint8_t tei, uint8_t sapi)
585{
586 struct e1inp_sign_link *link;
587 int ret = 0;
588
589 switch (ts->type) {
590 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200591 /* we only need to write a 'Fake LAPD' packet here, if
592 * the underlying driver hides LAPD from us. If we use
593 * the libosmocore LAPD implementation, it will take
594 * care of writing the _actual_ LAPD packet */
595 if (!ts->lapd)
596 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200597 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200598 link = e1inp_lookup_sign_link(ts, tei, sapi);
599 if (!link) {
Harald Welteb5af0992020-01-12 12:59:52 +0100600 LOGPITS(ts, DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200601 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200602 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200603 return -EINVAL;
604 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200605 if (!ts->line->ops->sign_link) {
Harald Welteb5af0992020-01-12 12:59:52 +0100606 LOGPITS(ts, DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200607 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200608 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200609 return -ENOENT;
610 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200611 msg->dst = link;
612 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200613 break;
614 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200615 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200616 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200617 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200618 case E1INP_TS_TYPE_RAW:
619 ts->raw.recv_cb(ts, msg);
620 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200621 case E1INP_TS_TYPE_HDLC:
622 ts->hdlc.recv_cb(ts, msg);
623 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200624 default:
625 ret = -EINVAL;
Harald Welteb5af0992020-01-12 12:59:52 +0100626 LOGPITS(ts, DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200627 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200628 break;
629 }
630
631 return ret;
632}
633
Harald Weltefd44a5f2011-08-21 00:48:54 +0200634/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
635 * \param[in] e1i_ts E1 Timeslot data structure
636 * \param[in] msg Message buffer containing full LAPD message
637 *
638 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
639 * message first into our LAPD code. This allows a driver to read raw
640 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
641 * present in any underlying driver.
642 */
643int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
644{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200645 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200646 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200647
648 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200649 if ((msg->data[0] & 0x1))
650 tei = 0;
651 else
652 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200653
Harald Welteb5af0992020-01-12 12:59:52 +0100654 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200655
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200656 ret = lapd_receive(e1i_ts->lapd, msg, &error);
657 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200658 switch(error) {
659 case LAPD_ERR_UNKNOWN_TEI:
660 /* We don't know about this TEI, probably the BSC
661 * lost local states (it crashed or it was stopped),
662 * notify the driver to see if it can do anything to
663 * recover the existing signalling links with the BTS.
664 */
665 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
666 return -EIO;
667 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200668 }
669
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200670 return 0;
671}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200672
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200673void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
674 void *rx_cbdata)
675{
676 struct e1inp_ts *e1i_ts = rx_cbdata;
677 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200678
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200679 switch (dp->oph.primitive) {
680 case PRIM_DL_EST:
Harald Welteb5af0992020-01-12 12:59:52 +0100681 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200682 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200683 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200684 case PRIM_DL_REL:
Harald Welteb5af0992020-01-12 12:59:52 +0100685 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200686 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200687 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200688 case PRIM_DL_DATA:
689 case PRIM_DL_UNIT_DATA:
690 if (dp->oph.operation == PRIM_OP_INDICATION) {
691 msg->l2h = msg->l3h;
Harald Welteb5af0992020-01-12 12:59:52 +0100692 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "RX: %s sapi=%d tei=%d\n",
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200693 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
694 sapi, tei);
695 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
696 return;
697 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200698 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200699 case PRIM_MDL_ERROR:
Harald Welteb5af0992020-01-12 12:59:52 +0100700 LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200701 break;
702 default:
703 printf("ERROR: unknown prim\n");
704 break;
705 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200706
707 msgb_free(msg);
708
709 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200710}
711
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200712#define TSX_ALLOC_SIZE 4096
713
714/* called by driver if it wants to transmit on a given TS */
715struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
716 struct e1inp_sign_link **sign_link)
717{
718 struct e1inp_sign_link *link;
719 struct msgb *msg = NULL;
720 int len;
721
722 switch (e1i_ts->type) {
723 case E1INP_TS_TYPE_SIGN:
724 /* FIXME: implement this round robin */
725 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
726 msg = msgb_dequeue(&link->tx_list);
727 if (msg) {
728 if (sign_link)
729 *sign_link = link;
730 break;
731 }
732 }
733 break;
734 case E1INP_TS_TYPE_TRAU:
735 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
736 if (!msg)
737 return NULL;
738 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200739 if (len != 40) {
Harald Welteb5af0992020-01-12 12:59:52 +0100740 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "cannot transmit, failed to mux\n");
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200741 msgb_free(msg);
742 return NULL;
743 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200744 msgb_put(msg, 40);
745 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200746 case E1INP_TS_TYPE_RAW:
747 /* Get msgb from tx_queue */
748 msg = msgb_dequeue(&e1i_ts->raw.tx_queue);
749 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200750 case E1INP_TS_TYPE_HDLC:
751 /* Get msgb from tx_queue */
752 msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue);
753 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200754 default:
Harald Welteb5af0992020-01-12 12:59:52 +0100755 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200756 return NULL;
757 }
758 return msg;
759}
760
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100761static int e1inp_int_snd_event(struct e1inp_ts *ts,
762 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200763{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200764 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200765 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200766 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200767 isd.link_type = link->type;
768 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100769 isd.tei = link->tei;
770 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200771
772 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200773 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200774 return 0;
775}
776
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100777
778/* called by driver in case some kind of link state event */
779int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
780{
781 struct e1inp_sign_link *link;
782
783 link = e1inp_lookup_sign_link(ts, tei, sapi);
784 if (!link)
785 return -EINVAL;
786 return e1inp_int_snd_event(ts, link, evt);
787}
788
789void e1inp_close_socket(struct e1inp_ts *ts,
790 struct e1inp_sign_link *sign_link,
791 struct osmo_fd *bfd)
792{
793 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
794 /* the first e1inp_sign_link_destroy call closes the socket. */
795 if (bfd->fd != -1) {
796 osmo_fd_unregister(bfd);
797 close(bfd->fd);
798 bfd->fd = -1;
799 }
800}
801
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200802/* register a driver with the E1 core */
803int e1inp_driver_register(struct e1inp_driver *drv)
804{
805 llist_add_tail(&drv->list, &e1inp_driver_list);
806 return 0;
807}
808
809struct e1inp_driver *e1inp_driver_find(const char *name)
810{
811 struct e1inp_driver *drv;
812
813 llist_for_each_entry(drv, &e1inp_driver_list, list) {
814 if (!strcasecmp(name, drv->name))
815 return drv;
816 }
817 return NULL;
818}
819
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200820int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200821{
822 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200823 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200824
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200825 e1inp_line_get(line);
826
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200827 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200828 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200829 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200830 rc = 0;
831
Harald Welte7f9d8512016-07-04 09:59:46 +0200832 /* Set the PCAP file descriptor for all timeslots that have
833 * software LAPD instances, to ensure the osmo_lapd_pcap code is
834 * used to write PCAP files (if requested) */
835 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
836 struct e1inp_ts *e1i_ts = &line->ts[i];
837 if (e1i_ts->lapd)
838 e1i_ts->lapd->pcap_fd = pcap_fd;
839 }
840
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200841 /* Send a signal to anyone who is interested in new lines being
842 * configured */
843 memset(&isd, 0, sizeof(isd));
844 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200845 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200846
847 return rc;
848}
849
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200850static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200851 void *handler_data, void *signal_data)
852{
Harald Weltecc2241b2011-07-19 16:06:06 +0200853 if (subsys != SS_L_GLOBAL ||
854 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200855 return 0;
856
857 if (pcap_fd) {
858 close(pcap_fd);
859 pcap_fd = -1;
860 }
861
862 return 0;
863}
864
Harald Weltecac78fe2017-05-25 19:13:13 +0200865const struct value_string e1inp_signal_names[] = {
866 { S_L_INP_NONE, "NONE" },
867 { S_L_INP_TEI_UP, "TEI-UP" },
868 { S_L_INP_TEI_DN, "TEI-DOWN" },
869 { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" },
870 { S_L_INP_LINE_INIT, "LINE-INIT" },
871 { S_L_INP_LINE_ALARM, "LINE-ALARM" },
872 { S_L_INP_LINE_NOALARM, "LINE-NOALARM" },
873 { 0, NULL }
874};
875
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200876void e1inp_misdn_init(void);
877void e1inp_dahdi_init(void);
878void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200879void e1inp_rs232_init(void);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100880void e1inp_unixsocket_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200881
882void e1inp_init(void)
883{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200884 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200885 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
886 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200887 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200888
889 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200890#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200891 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200892#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200893 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200894 e1inp_rs232_init();
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100895 e1inp_unixsocket_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200896}