blob: 3cf810f56ab04a16c29c91a206b1cd5eb1912cb4 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis interface to E1 */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include "internal.h"
Harald Welte3bc78852011-08-24 08:32:38 +020023#include "../config.h"
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020024
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <time.h>
Holger Hans Peter Freyther25474582017-01-23 19:49:07 +010031#include <fcntl.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020032#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020035
Harald Weltefd44a5f2011-08-21 00:48:54 +020036#include <osmocom/abis/lapd.h>
37
Harald Weltef2737fc2011-08-16 14:30:10 +020038#include <osmocom/core/linuxlist.h>
39#include <osmocom/core/talloc.h>
40#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020041#include <osmocom/core/logging.h>
42#include <osmocom/core/signal.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020043#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020044
45#define NUM_E1_TS 32
46
47static void *tall_e1inp_ctx;
48
49/* list of all E1 drivers */
50LLIST_HEAD(e1inp_driver_list);
51
52/* list of all E1 lines */
53LLIST_HEAD(e1inp_line_list);
54
55static void *tall_sigl_ctx;
56
Harald Weltef2737fc2011-08-16 14:30:10 +020057static const struct rate_ctr_desc e1inp_ctr_d[] = {
58 [E1I_CTR_HDLC_ABORT] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020059 "hdlc.abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020060 },
61 [E1I_CTR_HDLC_BADFCS] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020062 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020063 },
64 [E1I_CTR_HDLC_OVERR] = {
65 "hdlc.overrun", "HDLC Overrun"
66 },
67 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020068 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020069 },
70 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020071 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020072 },
73};
74
75static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
76 .group_name_prefix = "e1inp",
77 .group_description = "E1 Input subsystem",
78 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
79 .ctr_desc = e1inp_ctr_d,
80};
81
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020082/*
83 * pcap writing of the misdn load
84 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
85 */
86#define DLT_LINUX_LAPD 177
87#define PCAP_INPUT 0
88#define PCAP_OUTPUT 1
89
90struct pcap_hdr {
91 uint32_t magic_number;
92 uint16_t version_major;
93 uint16_t version_minor;
94 int32_t thiszone;
95 uint32_t sigfigs;
96 uint32_t snaplen;
97 uint32_t network;
98} __attribute__((packed));
99
100struct pcaprec_hdr {
101 uint32_t ts_sec;
102 uint32_t ts_usec;
103 uint32_t incl_len;
104 uint32_t orig_len;
105} __attribute__((packed));
106
107struct fake_linux_lapd_header {
108 uint16_t pkttype;
109 uint16_t hatype;
110 uint16_t halen;
111 uint64_t addr;
112 int16_t protocol;
113} __attribute__((packed));
114
115struct lapd_header {
116 uint8_t ea1 : 1;
117 uint8_t cr : 1;
118 uint8_t sapi : 6;
119 uint8_t ea2 : 1;
120 uint8_t tei : 7;
121 uint8_t control_foo; /* fake UM's ... */
122} __attribute__((packed));
123
124osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
125osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
126osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
127osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
128osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
129
130
131static int pcap_fd = -1;
132
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200133int e1_set_pcap_fd(int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200134{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200135 struct pcap_hdr header = {
136 .magic_number = 0xa1b2c3d4,
137 .version_major = 2,
138 .version_minor = 4,
139 .thiszone = 0,
140 .sigfigs = 0,
141 .snaplen = 65535,
142 .network = DLT_LINUX_LAPD,
143 };
144
145 pcap_fd = fd;
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200146 return write(pcap_fd, &header, sizeof(header));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200147}
148
149/* This currently only works for the D-Channel */
150static void write_pcap_packet(int direction, int sapi, int tei,
151 struct msgb *msg) {
152 if (pcap_fd < 0)
153 return;
154
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200155 time_t cur_time;
156 struct tm *tm;
157
158 struct fake_linux_lapd_header header = {
159 .pkttype = 4,
160 .hatype = 0,
161 .halen = 0,
162 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
163 .protocol = ntohs(48),
164 };
165
166 struct lapd_header lapd_header = {
167 .ea1 = 0,
168 .cr = direction == PCAP_OUTPUT ? 1 : 0,
169 .sapi = sapi & 0x3F,
170 .ea2 = 1,
171 .tei = tei & 0x7F,
172 .control_foo = 0x03 /* UI */,
173 };
174
175 struct pcaprec_hdr payload_header = {
176 .ts_sec = 0,
177 .ts_usec = 0,
178 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
179 + sizeof(struct lapd_header),
180 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
181 + sizeof(struct lapd_header),
182 };
183
184
185 cur_time = time(NULL);
186 tm = localtime(&cur_time);
187 payload_header.ts_sec = mktime(tm);
188
Harald Weltec9295ea2014-08-21 02:41:41 +0200189 write(pcap_fd, &payload_header, sizeof(payload_header));
190 write(pcap_fd, &header, sizeof(header));
191 write(pcap_fd, &lapd_header, sizeof(lapd_header));
192 write(pcap_fd, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200193}
194
Harald Welte4ca5c532016-07-27 23:05:03 +0200195const struct value_string e1inp_sign_type_names[5] = {
196 { E1INP_SIGN_NONE, "None" },
197 { E1INP_SIGN_OML, "OML" },
198 { E1INP_SIGN_RSL, "RSL" },
199 { E1INP_SIGN_OSMO, "OSMO" },
200 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200201};
Harald Welte4ca5c532016-07-27 23:05:03 +0200202
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200203const char *e1inp_signtype_name(enum e1inp_sign_type tp)
204{
Harald Welte4ca5c532016-07-27 23:05:03 +0200205 return get_value_string(e1inp_sign_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200206}
207
Harald Welte7a228eb2016-07-28 11:09:31 +0200208const struct value_string e1inp_ts_type_names[6] = {
Harald Welte4ca5c532016-07-27 23:05:03 +0200209 { E1INP_TS_TYPE_NONE, "None" },
210 { E1INP_TS_TYPE_SIGN, "Signalling" },
211 { E1INP_TS_TYPE_TRAU, "TRAU" },
Harald Weltea0108e72016-07-27 21:44:50 +0200212 { E1INP_TS_TYPE_RAW, "RAW" },
Harald Welte7a228eb2016-07-28 11:09:31 +0200213 { E1INP_TS_TYPE_HDLC, "HDLC" },
Harald Welte4ca5c532016-07-27 23:05:03 +0200214 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200215};
216
217const char *e1inp_tstype_name(enum e1inp_ts_type tp)
218{
Harald Welte4ca5c532016-07-27 23:05:03 +0200219 return get_value_string(e1inp_ts_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200220}
221
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200222int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200223{
224 struct e1inp_sign_link *sign_link = msg->dst;
225 struct e1inp_driver *e1inp_driver;
226 struct e1inp_ts *e1i_ts;
Alexander Couzens35daa672016-11-08 16:17:20 +0100227
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200228 msg->l2h = msg->data;
229
230 /* don't know how to route this message. */
231 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200232 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200233 osmo_hexdump(msg->data, msg->len));
234 talloc_free(msg);
235 return -EINVAL;
236 }
237 e1i_ts = sign_link->ts;
238 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
239 /* notify the driver we have something to write */
240 e1inp_driver = sign_link->ts->line->driver;
241 e1inp_driver->want_write(e1i_ts);
242 }
243 msgb_enqueue(&sign_link->tx_list, msg);
244
Harald Welte7f9d8512016-07-04 09:59:46 +0200245 /* we only need to write a 'Fake LAPD' packet here, if the
246 * underlying driver hides LAPD from us. If we use the
247 * libosmocore LAPD implementation, it will take care of writing
248 * the _actual_ LAPD packet */
249 if (!e1i_ts->lapd) {
250 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi,
251 sign_link->tei, msg);
252 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200253
254 return 0;
255}
256
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200257int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200258{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200259 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200260}
261
262/* Timeslot */
263int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200264 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
265 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200266{
267 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
268 return 0;
269
270 ts->type = E1INP_TS_TYPE_TRAU;
271 ts->line = line;
272
273 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200274 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200275 ts->trau.demux.data = ts;
276 subch_demux_init(&ts->trau.demux);
277 return 0;
278}
279
280int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
281{
282 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
283 return 0;
284
285 ts->type = E1INP_TS_TYPE_SIGN;
286 ts->line = line;
287
288 if (line && line->driver)
289 ts->sign.delay = line->driver->default_delay;
290 else
291 ts->sign.delay = 100000;
292 INIT_LLIST_HEAD(&ts->sign.sign_links);
293 return 0;
294}
295
Harald Weltea0108e72016-07-27 21:44:50 +0200296int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line,
297 void (*raw_recv_cb)(struct e1inp_ts *ts,
298 struct msgb *msg))
299{
300 if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line)
301 return 0;
302
303 ts->type = E1INP_TS_TYPE_RAW;
304 ts->line = line;
305 ts->raw.recv_cb = raw_recv_cb;
306 INIT_LLIST_HEAD(&ts->raw.tx_queue);
307
308 return 0;
309}
310
Harald Welte7a228eb2016-07-28 11:09:31 +0200311int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line,
312 void (*hdlc_recv_cb)(struct e1inp_ts *ts,
313 struct msgb *msg))
314{
315 if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line)
316 return 0;
317
318 ts->type = E1INP_TS_TYPE_HDLC;
319 ts->line = line;
320 ts->hdlc.recv_cb = hdlc_recv_cb;
321 INIT_LLIST_HEAD(&ts->hdlc.tx_queue);
322
323 return 0;
324}
325
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200326struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200327{
328 struct e1inp_line *e1i_line;
329
330 /* iterate over global list of e1 lines */
331 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
332 if (e1i_line->num == e1_nr)
333 return e1i_line;
334 }
335 return NULL;
336}
337
338struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200339e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200340{
341 struct e1inp_driver *driver;
342 struct e1inp_line *line;
343 int i;
344
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200345 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200347 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348 e1_nr);
349 return NULL;
350 }
351
352 driver = e1inp_driver_find(driver_name);
353 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200354 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355 driver_name);
356 return NULL;
357 }
358
359 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
360 if (!line)
361 return NULL;
362
363 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200365
366 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
Harald Welteff8eed22017-07-12 00:38:25 +0200367 if (!line->rate_ctr) {
368 LOGP(DLINP, LOGL_ERROR, "Cannot allocate counter group\n");
369 talloc_free(line);
370 return NULL;
371 }
Harald Weltef2737fc2011-08-16 14:30:10 +0200372
Harald Weltec2889512011-09-13 23:49:04 +0100373 line->num_ts = NUM_E1_TS;
374 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200375 line->ts[i].num = i+1;
376 line->ts[i].line = line;
377 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200378 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200379 llist_add_tail(&line->list, &e1inp_line_list);
380
381 return line;
382}
383
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200384struct e1inp_line *
385e1inp_line_clone(void *ctx, struct e1inp_line *line)
386{
387 struct e1inp_line *clone;
388
389 /* clone virtual E1 line for this new OML link. */
390 clone = talloc_zero(ctx, struct e1inp_line);
391 if (clone == NULL)
392 return NULL;
393
394 memcpy(clone, line, sizeof(struct e1inp_line));
395 clone->refcnt = 1;
396 return clone;
397}
398
399void e1inp_line_get(struct e1inp_line *line)
400{
401 line->refcnt++;
402}
403
404void e1inp_line_put(struct e1inp_line *line)
405{
406 line->refcnt--;
407 if (line->refcnt == 0)
408 talloc_free(line);
409}
410
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200411void
412e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
413{
414 line->ops = ops;
415}
416
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200417#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200418struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200419{
420 struct e1inp_line *line;
421 int i;
422
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200423 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200424 if (line)
425 return line;
426
427 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
428 if (!line)
429 return NULL;
430
431 line->num = e1_nr;
432 for (i = 0; i < NUM_E1_TS; i++) {
433 line->ts[i].num = i+1;
434 line->ts[i].line = line;
435 }
436 llist_add_tail(&line->list, &e1inp_line_list);
437
438 return line;
439}
440#endif
441
442static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
443{
444 struct e1inp_line *e1i_line;
445
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200446 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200447 if (!e1i_line)
448 return NULL;
449
450 return &e1i_line->ts[ts_nr-1];
451}
452
453struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
454{
455 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
456
457 if (!e1i_ts)
458 return NULL;
459
460 return &e1i_ts->trau.mux;
461}
462
463/* Signalling Link */
464
465struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
466 uint8_t tei, uint8_t sapi)
467{
468 struct e1inp_sign_link *link;
469
470 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
471 if (link->sapi == sapi && link->tei == tei)
472 return link;
473 }
474
475 return NULL;
476}
477
478/* create a new signalling link in a E1 timeslot */
479
480struct e1inp_sign_link *
481e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
482 struct gsm_bts_trx *trx, uint8_t tei,
483 uint8_t sapi)
484{
485 struct e1inp_sign_link *link;
486
487 if (ts->type != E1INP_TS_TYPE_SIGN)
488 return NULL;
489
490 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
491 if (!link)
492 return NULL;
493
494 link->ts = ts;
495 link->type = type;
496 INIT_LLIST_HEAD(&link->tx_list);
497 link->trx = trx;
498 link->tei = tei;
499 link->sapi = sapi;
500
501 llist_add_tail(&link->list, &ts->sign.sign_links);
502
503 return link;
504}
505
506void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
507{
508 struct msgb *msg;
509
510 llist_del(&link->list);
511 while (!llist_empty(&link->tx_list)) {
512 msg = msgb_dequeue(&link->tx_list);
513 msgb_free(msg);
514 }
515
516 if (link->ts->type == E1INP_TS_TYPE_SIGN)
517 osmo_timer_del(&link->ts->sign.tx_timer);
518
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200519 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200520 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200521
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200522 e1inp_line_put(link->ts->line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200523 talloc_free(link);
524}
525
526/* XXX */
527/* the E1 driver tells us he has received something on a TS */
528int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
529 uint8_t tei, uint8_t sapi)
530{
531 struct e1inp_sign_link *link;
532 int ret = 0;
533
534 switch (ts->type) {
535 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200536 /* we only need to write a 'Fake LAPD' packet here, if
537 * the underlying driver hides LAPD from us. If we use
538 * the libosmocore LAPD implementation, it will take
539 * care of writing the _actual_ LAPD packet */
540 if (!ts->lapd)
541 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200542 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200543 link = e1inp_lookup_sign_link(ts, tei, sapi);
544 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200545 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200546 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200547 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200548 return -EINVAL;
549 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200550 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200551 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200552 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200553 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200554 return -ENOENT;
555 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200556 msg->dst = link;
557 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200558 break;
559 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200560 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200561 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200562 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200563 case E1INP_TS_TYPE_RAW:
564 ts->raw.recv_cb(ts, msg);
565 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200566 case E1INP_TS_TYPE_HDLC:
567 ts->hdlc.recv_cb(ts, msg);
568 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200569 default:
570 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200571 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200572 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573 break;
574 }
575
576 return ret;
577}
578
Harald Weltefd44a5f2011-08-21 00:48:54 +0200579/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
580 * \param[in] e1i_ts E1 Timeslot data structure
581 * \param[in] msg Message buffer containing full LAPD message
582 *
583 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
584 * message first into our LAPD code. This allows a driver to read raw
585 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
586 * present in any underlying driver.
587 */
588int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
589{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200590 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200591 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200592
593 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200594 if ((msg->data[0] & 0x1))
595 tei = 0;
596 else
597 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200598
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200599 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200600
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200601 ret = lapd_receive(e1i_ts->lapd, msg, &error);
602 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200603 switch(error) {
604 case LAPD_ERR_UNKNOWN_TEI:
605 /* We don't know about this TEI, probably the BSC
606 * lost local states (it crashed or it was stopped),
607 * notify the driver to see if it can do anything to
608 * recover the existing signalling links with the BTS.
609 */
610 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
611 return -EIO;
612 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200613 }
614
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200615 return 0;
616}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200617
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200618void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
619 void *rx_cbdata)
620{
621 struct e1inp_ts *e1i_ts = rx_cbdata;
622 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200623
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200624 switch (dp->oph.primitive) {
625 case PRIM_DL_EST:
626 DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
627 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200628 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200629 case PRIM_DL_REL:
630 DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
631 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200632 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200633 case PRIM_DL_DATA:
634 case PRIM_DL_UNIT_DATA:
635 if (dp->oph.operation == PRIM_OP_INDICATION) {
636 msg->l2h = msg->l3h;
637 DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n",
638 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
639 sapi, tei);
640 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
641 return;
642 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200643 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200644 case PRIM_MDL_ERROR:
645 DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200646 break;
647 default:
648 printf("ERROR: unknown prim\n");
649 break;
650 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200651
652 msgb_free(msg);
653
654 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200655}
656
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200657#define TSX_ALLOC_SIZE 4096
658
659/* called by driver if it wants to transmit on a given TS */
660struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
661 struct e1inp_sign_link **sign_link)
662{
663 struct e1inp_sign_link *link;
664 struct msgb *msg = NULL;
665 int len;
666
667 switch (e1i_ts->type) {
668 case E1INP_TS_TYPE_SIGN:
669 /* FIXME: implement this round robin */
670 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
671 msg = msgb_dequeue(&link->tx_list);
672 if (msg) {
673 if (sign_link)
674 *sign_link = link;
675 break;
676 }
677 }
678 break;
679 case E1INP_TS_TYPE_TRAU:
680 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
681 if (!msg)
682 return NULL;
683 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200684 if (len != 40) {
685 LOGP(DLMI, LOGL_ERROR,
686 "cannot transmit, failed to mux\n");
687 msgb_free(msg);
688 return NULL;
689 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200690 msgb_put(msg, 40);
691 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200692 case E1INP_TS_TYPE_RAW:
693 /* Get msgb from tx_queue */
694 msg = msgb_dequeue(&e1i_ts->raw.tx_queue);
695 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200696 case E1INP_TS_TYPE_HDLC:
697 /* Get msgb from tx_queue */
698 msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue);
699 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200700 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200701 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200702 return NULL;
703 }
704 return msg;
705}
706
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100707static int e1inp_int_snd_event(struct e1inp_ts *ts,
708 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200709{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200710 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200711 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200712 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200713 isd.link_type = link->type;
714 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100715 isd.tei = link->tei;
716 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200717
718 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200719 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200720 return 0;
721}
722
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100723
724/* called by driver in case some kind of link state event */
725int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
726{
727 struct e1inp_sign_link *link;
728
729 link = e1inp_lookup_sign_link(ts, tei, sapi);
730 if (!link)
731 return -EINVAL;
732 return e1inp_int_snd_event(ts, link, evt);
733}
734
735void e1inp_close_socket(struct e1inp_ts *ts,
736 struct e1inp_sign_link *sign_link,
737 struct osmo_fd *bfd)
738{
739 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
740 /* the first e1inp_sign_link_destroy call closes the socket. */
741 if (bfd->fd != -1) {
742 osmo_fd_unregister(bfd);
743 close(bfd->fd);
744 bfd->fd = -1;
745 }
746}
747
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200748/* register a driver with the E1 core */
749int e1inp_driver_register(struct e1inp_driver *drv)
750{
751 llist_add_tail(&drv->list, &e1inp_driver_list);
752 return 0;
753}
754
755struct e1inp_driver *e1inp_driver_find(const char *name)
756{
757 struct e1inp_driver *drv;
758
759 llist_for_each_entry(drv, &e1inp_driver_list, list) {
760 if (!strcasecmp(name, drv->name))
761 return drv;
762 }
763 return NULL;
764}
765
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200766int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200767{
768 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200769 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200770
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200771 e1inp_line_get(line);
772
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200773 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200774 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200775 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200776 rc = 0;
777
Harald Welte7f9d8512016-07-04 09:59:46 +0200778 /* Set the PCAP file descriptor for all timeslots that have
779 * software LAPD instances, to ensure the osmo_lapd_pcap code is
780 * used to write PCAP files (if requested) */
781 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
782 struct e1inp_ts *e1i_ts = &line->ts[i];
783 if (e1i_ts->lapd)
784 e1i_ts->lapd->pcap_fd = pcap_fd;
785 }
786
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200787 /* Send a signal to anyone who is interested in new lines being
788 * configured */
789 memset(&isd, 0, sizeof(isd));
790 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200791 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200792
793 return rc;
794}
795
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200796static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200797 void *handler_data, void *signal_data)
798{
Harald Weltecc2241b2011-07-19 16:06:06 +0200799 if (subsys != SS_L_GLOBAL ||
800 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200801 return 0;
802
803 if (pcap_fd) {
804 close(pcap_fd);
805 pcap_fd = -1;
806 }
807
808 return 0;
809}
810
Harald Weltecac78fe2017-05-25 19:13:13 +0200811const struct value_string e1inp_signal_names[] = {
812 { S_L_INP_NONE, "NONE" },
813 { S_L_INP_TEI_UP, "TEI-UP" },
814 { S_L_INP_TEI_DN, "TEI-DOWN" },
815 { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" },
816 { S_L_INP_LINE_INIT, "LINE-INIT" },
817 { S_L_INP_LINE_ALARM, "LINE-ALARM" },
818 { S_L_INP_LINE_NOALARM, "LINE-NOALARM" },
819 { 0, NULL }
820};
821
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200822void e1inp_misdn_init(void);
823void e1inp_dahdi_init(void);
824void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200825void e1inp_rs232_init(void);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100826void e1inp_unixsocket_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200827
828void e1inp_init(void)
829{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200830 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200831 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
832 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200833 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200834
835 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200836#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200837 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200838#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200839 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200840 e1inp_rs232_init();
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100841 e1inp_unixsocket_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200842}