blob: 11949a118681a9f2be25e32ddaccbd407df8daf4 [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));
Stefan Sperlingb0162072018-05-22 18:19:00 +0200397
398 if (line->name) {
399 clone->name = talloc_strdup(clone, line->name);
400 OSMO_ASSERT(clone->name);
401 }
402 if (line->sock_path) {
403 clone->sock_path = talloc_strdup(clone, line->sock_path);
404 OSMO_ASSERT(clone->sock_path);
405 }
406
407 /*
408 * Rate counters and driver data are shared between clones. These are pointers
409 * to dynamic memory so we use reference counting to avoid a double-free (see OS#3137).
410 */
411 OSMO_ASSERT(line->rate_ctr);
412 clone->rate_ctr = talloc_reference(clone, line->rate_ctr);
413 if (line->driver_data)
414 clone->driver_data = talloc_reference(clone, line->driver_data);
415
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200416 clone->refcnt = 1;
417 return clone;
418}
419
420void e1inp_line_get(struct e1inp_line *line)
421{
422 line->refcnt++;
423}
424
425void e1inp_line_put(struct e1inp_line *line)
426{
427 line->refcnt--;
Stefan Sperlingb0162072018-05-22 18:19:00 +0200428 if (line->refcnt == 0) {
429 /* Remove our counter group from libosmocore's global counter
430 * list if we are freeing the last remaining talloc context.
431 * Otherwise we get a use-after-free when libosmocore's timer
432 * ticks again and attempts to update these counters (OS#3011).
433 *
434 * Note that talloc internally counts "secondary" references
435 * _in addition to_ the initial allocation context, so yes,
436 * we must check for *zero* remaining secondary contexts here. */
437 if (talloc_reference_count(line->rate_ctr) == 0) {
438 rate_ctr_group_free(line->rate_ctr);
439 } else {
440 /* We are not freeing the last talloc context.
441 * Instead of calling talloc_free(), unlink this 'line' pointer
442 * which serves as one of several talloc contexts for the rate
443 * counters and driver private state. */
444 talloc_unlink(line, line->rate_ctr);
445 if (line->driver_data)
446 talloc_unlink(line, line->driver_data);
447 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200448 talloc_free(line);
Stefan Sperlingb0162072018-05-22 18:19:00 +0200449 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200450}
451
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200452void
453e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
454{
455 line->ops = ops;
456}
457
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200458#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200459struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200460{
461 struct e1inp_line *line;
462 int i;
463
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200464 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200465 if (line)
466 return line;
467
468 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
469 if (!line)
470 return NULL;
471
472 line->num = e1_nr;
473 for (i = 0; i < NUM_E1_TS; i++) {
474 line->ts[i].num = i+1;
475 line->ts[i].line = line;
476 }
477 llist_add_tail(&line->list, &e1inp_line_list);
478
479 return line;
480}
481#endif
482
483static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
484{
485 struct e1inp_line *e1i_line;
486
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200487 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200488 if (!e1i_line)
489 return NULL;
490
491 return &e1i_line->ts[ts_nr-1];
492}
493
494struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
495{
496 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
497
498 if (!e1i_ts)
499 return NULL;
500
501 return &e1i_ts->trau.mux;
502}
503
504/* Signalling Link */
505
506struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
507 uint8_t tei, uint8_t sapi)
508{
509 struct e1inp_sign_link *link;
510
511 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
512 if (link->sapi == sapi && link->tei == tei)
513 return link;
514 }
515
516 return NULL;
517}
518
519/* create a new signalling link in a E1 timeslot */
520
521struct e1inp_sign_link *
522e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
523 struct gsm_bts_trx *trx, uint8_t tei,
524 uint8_t sapi)
525{
526 struct e1inp_sign_link *link;
527
528 if (ts->type != E1INP_TS_TYPE_SIGN)
529 return NULL;
530
531 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
532 if (!link)
533 return NULL;
534
535 link->ts = ts;
536 link->type = type;
537 INIT_LLIST_HEAD(&link->tx_list);
538 link->trx = trx;
539 link->tei = tei;
540 link->sapi = sapi;
541
542 llist_add_tail(&link->list, &ts->sign.sign_links);
543
544 return link;
545}
546
547void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
548{
549 struct msgb *msg;
550
551 llist_del(&link->list);
552 while (!llist_empty(&link->tx_list)) {
553 msg = msgb_dequeue(&link->tx_list);
554 msgb_free(msg);
555 }
556
557 if (link->ts->type == E1INP_TS_TYPE_SIGN)
558 osmo_timer_del(&link->ts->sign.tx_timer);
559
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200560 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200561 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200562
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200563 e1inp_line_put(link->ts->line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200564 talloc_free(link);
565}
566
567/* XXX */
568/* the E1 driver tells us he has received something on a TS */
569int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
570 uint8_t tei, uint8_t sapi)
571{
572 struct e1inp_sign_link *link;
573 int ret = 0;
574
575 switch (ts->type) {
576 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200577 /* we only need to write a 'Fake LAPD' packet here, if
578 * the underlying driver hides LAPD from us. If we use
579 * the libosmocore LAPD implementation, it will take
580 * care of writing the _actual_ LAPD packet */
581 if (!ts->lapd)
582 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200583 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200584 link = e1inp_lookup_sign_link(ts, tei, sapi);
585 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200586 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200587 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200588 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200589 return -EINVAL;
590 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200591 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200592 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200593 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200594 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200595 return -ENOENT;
596 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200597 msg->dst = link;
598 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200599 break;
600 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200601 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200602 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200603 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200604 case E1INP_TS_TYPE_RAW:
605 ts->raw.recv_cb(ts, msg);
606 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200607 case E1INP_TS_TYPE_HDLC:
608 ts->hdlc.recv_cb(ts, msg);
609 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200610 default:
611 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200612 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200613 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200614 break;
615 }
616
617 return ret;
618}
619
Harald Weltefd44a5f2011-08-21 00:48:54 +0200620/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
621 * \param[in] e1i_ts E1 Timeslot data structure
622 * \param[in] msg Message buffer containing full LAPD message
623 *
624 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
625 * message first into our LAPD code. This allows a driver to read raw
626 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
627 * present in any underlying driver.
628 */
629int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
630{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200631 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200632 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200633
634 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200635 if ((msg->data[0] & 0x1))
636 tei = 0;
637 else
638 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200639
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200640 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200641
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200642 ret = lapd_receive(e1i_ts->lapd, msg, &error);
643 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200644 switch(error) {
645 case LAPD_ERR_UNKNOWN_TEI:
646 /* We don't know about this TEI, probably the BSC
647 * lost local states (it crashed or it was stopped),
648 * notify the driver to see if it can do anything to
649 * recover the existing signalling links with the BTS.
650 */
651 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
652 return -EIO;
653 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200654 }
655
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200656 return 0;
657}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200658
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200659void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
660 void *rx_cbdata)
661{
662 struct e1inp_ts *e1i_ts = rx_cbdata;
663 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200664
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200665 switch (dp->oph.primitive) {
666 case PRIM_DL_EST:
667 DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
668 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200669 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200670 case PRIM_DL_REL:
671 DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
672 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200673 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200674 case PRIM_DL_DATA:
675 case PRIM_DL_UNIT_DATA:
676 if (dp->oph.operation == PRIM_OP_INDICATION) {
677 msg->l2h = msg->l3h;
678 DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n",
679 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
680 sapi, tei);
681 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
682 return;
683 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200684 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200685 case PRIM_MDL_ERROR:
686 DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200687 break;
688 default:
689 printf("ERROR: unknown prim\n");
690 break;
691 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200692
693 msgb_free(msg);
694
695 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200696}
697
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200698#define TSX_ALLOC_SIZE 4096
699
700/* called by driver if it wants to transmit on a given TS */
701struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
702 struct e1inp_sign_link **sign_link)
703{
704 struct e1inp_sign_link *link;
705 struct msgb *msg = NULL;
706 int len;
707
708 switch (e1i_ts->type) {
709 case E1INP_TS_TYPE_SIGN:
710 /* FIXME: implement this round robin */
711 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
712 msg = msgb_dequeue(&link->tx_list);
713 if (msg) {
714 if (sign_link)
715 *sign_link = link;
716 break;
717 }
718 }
719 break;
720 case E1INP_TS_TYPE_TRAU:
721 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
722 if (!msg)
723 return NULL;
724 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200725 if (len != 40) {
726 LOGP(DLMI, LOGL_ERROR,
727 "cannot transmit, failed to mux\n");
728 msgb_free(msg);
729 return NULL;
730 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200731 msgb_put(msg, 40);
732 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200733 case E1INP_TS_TYPE_RAW:
734 /* Get msgb from tx_queue */
735 msg = msgb_dequeue(&e1i_ts->raw.tx_queue);
736 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200737 case E1INP_TS_TYPE_HDLC:
738 /* Get msgb from tx_queue */
739 msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue);
740 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200741 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200742 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200743 return NULL;
744 }
745 return msg;
746}
747
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100748static int e1inp_int_snd_event(struct e1inp_ts *ts,
749 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200750{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200751 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200752 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200753 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200754 isd.link_type = link->type;
755 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100756 isd.tei = link->tei;
757 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200758
759 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200760 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200761 return 0;
762}
763
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100764
765/* called by driver in case some kind of link state event */
766int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
767{
768 struct e1inp_sign_link *link;
769
770 link = e1inp_lookup_sign_link(ts, tei, sapi);
771 if (!link)
772 return -EINVAL;
773 return e1inp_int_snd_event(ts, link, evt);
774}
775
776void e1inp_close_socket(struct e1inp_ts *ts,
777 struct e1inp_sign_link *sign_link,
778 struct osmo_fd *bfd)
779{
780 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
781 /* the first e1inp_sign_link_destroy call closes the socket. */
782 if (bfd->fd != -1) {
783 osmo_fd_unregister(bfd);
784 close(bfd->fd);
785 bfd->fd = -1;
786 }
787}
788
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200789/* register a driver with the E1 core */
790int e1inp_driver_register(struct e1inp_driver *drv)
791{
792 llist_add_tail(&drv->list, &e1inp_driver_list);
793 return 0;
794}
795
796struct e1inp_driver *e1inp_driver_find(const char *name)
797{
798 struct e1inp_driver *drv;
799
800 llist_for_each_entry(drv, &e1inp_driver_list, list) {
801 if (!strcasecmp(name, drv->name))
802 return drv;
803 }
804 return NULL;
805}
806
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200807int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200808{
809 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200810 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200811
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200812 e1inp_line_get(line);
813
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200814 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200815 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200816 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200817 rc = 0;
818
Harald Welte7f9d8512016-07-04 09:59:46 +0200819 /* Set the PCAP file descriptor for all timeslots that have
820 * software LAPD instances, to ensure the osmo_lapd_pcap code is
821 * used to write PCAP files (if requested) */
822 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
823 struct e1inp_ts *e1i_ts = &line->ts[i];
824 if (e1i_ts->lapd)
825 e1i_ts->lapd->pcap_fd = pcap_fd;
826 }
827
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200828 /* Send a signal to anyone who is interested in new lines being
829 * configured */
830 memset(&isd, 0, sizeof(isd));
831 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200832 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200833
834 return rc;
835}
836
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200837static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200838 void *handler_data, void *signal_data)
839{
Harald Weltecc2241b2011-07-19 16:06:06 +0200840 if (subsys != SS_L_GLOBAL ||
841 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200842 return 0;
843
844 if (pcap_fd) {
845 close(pcap_fd);
846 pcap_fd = -1;
847 }
848
849 return 0;
850}
851
Harald Weltecac78fe2017-05-25 19:13:13 +0200852const struct value_string e1inp_signal_names[] = {
853 { S_L_INP_NONE, "NONE" },
854 { S_L_INP_TEI_UP, "TEI-UP" },
855 { S_L_INP_TEI_DN, "TEI-DOWN" },
856 { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" },
857 { S_L_INP_LINE_INIT, "LINE-INIT" },
858 { S_L_INP_LINE_ALARM, "LINE-ALARM" },
859 { S_L_INP_LINE_NOALARM, "LINE-NOALARM" },
860 { 0, NULL }
861};
862
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200863void e1inp_misdn_init(void);
864void e1inp_dahdi_init(void);
865void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200866void e1inp_rs232_init(void);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100867void e1inp_unixsocket_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200868
869void e1inp_init(void)
870{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200871 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200872 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
873 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200874 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200875
876 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200877#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200878 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200879#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200880 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200881 e1inp_rs232_init();
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100882 e1inp_unixsocket_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200883}