blob: 2c8a541cd406295928f311162505f2f2ebcccb8c [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>
31#include <sys/fcntl.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35#include <mISDNif.h>
36
Harald Weltefd44a5f2011-08-21 00:48:54 +020037#include <osmocom/abis/lapd.h>
38
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020039//#define AF_COMPATIBILITY_FUNC
40//#include <compat_af_isdn.h>
41#ifndef AF_ISDN
42#define AF_ISDN 34
43#define PF_ISDN AF_ISDN
44#endif
45
Harald Weltef2737fc2011-08-16 14:30:10 +020046#include <osmocom/core/linuxlist.h>
47#include <osmocom/core/talloc.h>
48#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049#include <osmocom/core/logging.h>
50#include <osmocom/core/signal.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052
53#define NUM_E1_TS 32
54
55static void *tall_e1inp_ctx;
56
57/* list of all E1 drivers */
58LLIST_HEAD(e1inp_driver_list);
59
60/* list of all E1 lines */
61LLIST_HEAD(e1inp_line_list);
62
63static void *tall_sigl_ctx;
64
Harald Weltef2737fc2011-08-16 14:30:10 +020065static const struct rate_ctr_desc e1inp_ctr_d[] = {
66 [E1I_CTR_HDLC_ABORT] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020067 "hdlc.abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020068 },
69 [E1I_CTR_HDLC_BADFCS] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020070 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020071 },
72 [E1I_CTR_HDLC_OVERR] = {
73 "hdlc.overrun", "HDLC Overrun"
74 },
75 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020076 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020077 },
78 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020079 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020080 },
81};
82
83static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
84 .group_name_prefix = "e1inp",
85 .group_description = "E1 Input subsystem",
86 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
87 .ctr_desc = e1inp_ctr_d,
88};
89
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020090/*
91 * pcap writing of the misdn load
92 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
93 */
94#define DLT_LINUX_LAPD 177
95#define PCAP_INPUT 0
96#define PCAP_OUTPUT 1
97
98struct pcap_hdr {
99 uint32_t magic_number;
100 uint16_t version_major;
101 uint16_t version_minor;
102 int32_t thiszone;
103 uint32_t sigfigs;
104 uint32_t snaplen;
105 uint32_t network;
106} __attribute__((packed));
107
108struct pcaprec_hdr {
109 uint32_t ts_sec;
110 uint32_t ts_usec;
111 uint32_t incl_len;
112 uint32_t orig_len;
113} __attribute__((packed));
114
115struct fake_linux_lapd_header {
116 uint16_t pkttype;
117 uint16_t hatype;
118 uint16_t halen;
119 uint64_t addr;
120 int16_t protocol;
121} __attribute__((packed));
122
123struct lapd_header {
124 uint8_t ea1 : 1;
125 uint8_t cr : 1;
126 uint8_t sapi : 6;
127 uint8_t ea2 : 1;
128 uint8_t tei : 7;
129 uint8_t control_foo; /* fake UM's ... */
130} __attribute__((packed));
131
132osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
133osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
134osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
135osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
136osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
137
138
139static int pcap_fd = -1;
140
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200141int e1_set_pcap_fd(int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200142{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200143 struct pcap_hdr header = {
144 .magic_number = 0xa1b2c3d4,
145 .version_major = 2,
146 .version_minor = 4,
147 .thiszone = 0,
148 .sigfigs = 0,
149 .snaplen = 65535,
150 .network = DLT_LINUX_LAPD,
151 };
152
153 pcap_fd = fd;
Pablo Neira Ayuso2e11f5c2013-07-05 15:08:18 +0200154 return write(pcap_fd, &header, sizeof(header));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200155}
156
157/* This currently only works for the D-Channel */
158static void write_pcap_packet(int direction, int sapi, int tei,
159 struct msgb *msg) {
160 if (pcap_fd < 0)
161 return;
162
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200163 time_t cur_time;
164 struct tm *tm;
165
166 struct fake_linux_lapd_header header = {
167 .pkttype = 4,
168 .hatype = 0,
169 .halen = 0,
170 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
171 .protocol = ntohs(48),
172 };
173
174 struct lapd_header lapd_header = {
175 .ea1 = 0,
176 .cr = direction == PCAP_OUTPUT ? 1 : 0,
177 .sapi = sapi & 0x3F,
178 .ea2 = 1,
179 .tei = tei & 0x7F,
180 .control_foo = 0x03 /* UI */,
181 };
182
183 struct pcaprec_hdr payload_header = {
184 .ts_sec = 0,
185 .ts_usec = 0,
186 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
187 + sizeof(struct lapd_header),
188 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
189 + sizeof(struct lapd_header),
190 };
191
192
193 cur_time = time(NULL);
194 tm = localtime(&cur_time);
195 payload_header.ts_sec = mktime(tm);
196
Harald Weltec9295ea2014-08-21 02:41:41 +0200197 write(pcap_fd, &payload_header, sizeof(payload_header));
198 write(pcap_fd, &header, sizeof(header));
199 write(pcap_fd, &lapd_header, sizeof(lapd_header));
200 write(pcap_fd, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200201}
202
Harald Welte4ca5c532016-07-27 23:05:03 +0200203const struct value_string e1inp_sign_type_names[5] = {
204 { E1INP_SIGN_NONE, "None" },
205 { E1INP_SIGN_OML, "OML" },
206 { E1INP_SIGN_RSL, "RSL" },
207 { E1INP_SIGN_OSMO, "OSMO" },
208 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200209};
Harald Welte4ca5c532016-07-27 23:05:03 +0200210
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200211const char *e1inp_signtype_name(enum e1inp_sign_type tp)
212{
Harald Welte4ca5c532016-07-27 23:05:03 +0200213 return get_value_string(e1inp_sign_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200214}
215
Harald Welte7a228eb2016-07-28 11:09:31 +0200216const struct value_string e1inp_ts_type_names[6] = {
Harald Welte4ca5c532016-07-27 23:05:03 +0200217 { E1INP_TS_TYPE_NONE, "None" },
218 { E1INP_TS_TYPE_SIGN, "Signalling" },
219 { E1INP_TS_TYPE_TRAU, "TRAU" },
Harald Weltea0108e72016-07-27 21:44:50 +0200220 { E1INP_TS_TYPE_RAW, "RAW" },
Harald Welte7a228eb2016-07-28 11:09:31 +0200221 { E1INP_TS_TYPE_HDLC, "HDLC" },
Harald Welte4ca5c532016-07-27 23:05:03 +0200222 { 0, NULL }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200223};
224
225const char *e1inp_tstype_name(enum e1inp_ts_type tp)
226{
Harald Welte4ca5c532016-07-27 23:05:03 +0200227 return get_value_string(e1inp_ts_type_names, tp);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200228}
229
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200230int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200231{
232 struct e1inp_sign_link *sign_link = msg->dst;
233 struct e1inp_driver *e1inp_driver;
234 struct e1inp_ts *e1i_ts;
Alexander Couzens35daa672016-11-08 16:17:20 +0100235
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200236 msg->l2h = msg->data;
237
238 /* don't know how to route this message. */
239 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200240 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200241 osmo_hexdump(msg->data, msg->len));
242 talloc_free(msg);
243 return -EINVAL;
244 }
245 e1i_ts = sign_link->ts;
246 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
247 /* notify the driver we have something to write */
248 e1inp_driver = sign_link->ts->line->driver;
249 e1inp_driver->want_write(e1i_ts);
250 }
251 msgb_enqueue(&sign_link->tx_list, msg);
252
Harald Welte7f9d8512016-07-04 09:59:46 +0200253 /* we only need to write a 'Fake LAPD' packet here, if the
254 * underlying driver hides LAPD from us. If we use the
255 * libosmocore LAPD implementation, it will take care of writing
256 * the _actual_ LAPD packet */
257 if (!e1i_ts->lapd) {
258 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi,
259 sign_link->tei, msg);
260 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200261
262 return 0;
263}
264
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200265int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200266{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200267 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200268}
269
270/* Timeslot */
271int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200272 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
273 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200274{
275 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
276 return 0;
277
278 ts->type = E1INP_TS_TYPE_TRAU;
279 ts->line = line;
280
281 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200282 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200283 ts->trau.demux.data = ts;
284 subch_demux_init(&ts->trau.demux);
285 return 0;
286}
287
288int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
289{
290 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
291 return 0;
292
293 ts->type = E1INP_TS_TYPE_SIGN;
294 ts->line = line;
295
296 if (line && line->driver)
297 ts->sign.delay = line->driver->default_delay;
298 else
299 ts->sign.delay = 100000;
300 INIT_LLIST_HEAD(&ts->sign.sign_links);
301 return 0;
302}
303
Harald Weltea0108e72016-07-27 21:44:50 +0200304int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line,
305 void (*raw_recv_cb)(struct e1inp_ts *ts,
306 struct msgb *msg))
307{
308 if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line)
309 return 0;
310
311 ts->type = E1INP_TS_TYPE_RAW;
312 ts->line = line;
313 ts->raw.recv_cb = raw_recv_cb;
314 INIT_LLIST_HEAD(&ts->raw.tx_queue);
315
316 return 0;
317}
318
Harald Welte7a228eb2016-07-28 11:09:31 +0200319int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line,
320 void (*hdlc_recv_cb)(struct e1inp_ts *ts,
321 struct msgb *msg))
322{
323 if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line)
324 return 0;
325
326 ts->type = E1INP_TS_TYPE_HDLC;
327 ts->line = line;
328 ts->hdlc.recv_cb = hdlc_recv_cb;
329 INIT_LLIST_HEAD(&ts->hdlc.tx_queue);
330
331 return 0;
332}
333
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200334struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200335{
336 struct e1inp_line *e1i_line;
337
338 /* iterate over global list of e1 lines */
339 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
340 if (e1i_line->num == e1_nr)
341 return e1i_line;
342 }
343 return NULL;
344}
345
346struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200347e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200348{
349 struct e1inp_driver *driver;
350 struct e1inp_line *line;
351 int i;
352
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200353 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200355 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200356 e1_nr);
357 return NULL;
358 }
359
360 driver = e1inp_driver_find(driver_name);
361 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200362 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200363 driver_name);
364 return NULL;
365 }
366
367 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
368 if (!line)
369 return NULL;
370
371 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200372 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200373
374 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
375
Harald Weltec2889512011-09-13 23:49:04 +0100376 line->num_ts = NUM_E1_TS;
377 for (i = 0; i < line->num_ts; i++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200378 line->ts[i].num = i+1;
379 line->ts[i].line = line;
380 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200381 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200382 llist_add_tail(&line->list, &e1inp_line_list);
383
384 return line;
385}
386
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200387struct e1inp_line *
388e1inp_line_clone(void *ctx, struct e1inp_line *line)
389{
390 struct e1inp_line *clone;
391
392 /* clone virtual E1 line for this new OML link. */
393 clone = talloc_zero(ctx, struct e1inp_line);
394 if (clone == NULL)
395 return NULL;
396
397 memcpy(clone, line, sizeof(struct e1inp_line));
398 clone->refcnt = 1;
399 return clone;
400}
401
402void e1inp_line_get(struct e1inp_line *line)
403{
404 line->refcnt++;
405}
406
407void e1inp_line_put(struct e1inp_line *line)
408{
409 line->refcnt--;
410 if (line->refcnt == 0)
411 talloc_free(line);
412}
413
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200414void
415e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
416{
417 line->ops = ops;
418}
419
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200420#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200421struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200422{
423 struct e1inp_line *line;
424 int i;
425
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200426 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200427 if (line)
428 return line;
429
430 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
431 if (!line)
432 return NULL;
433
434 line->num = e1_nr;
435 for (i = 0; i < NUM_E1_TS; i++) {
436 line->ts[i].num = i+1;
437 line->ts[i].line = line;
438 }
439 llist_add_tail(&line->list, &e1inp_line_list);
440
441 return line;
442}
443#endif
444
445static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
446{
447 struct e1inp_line *e1i_line;
448
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200449 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200450 if (!e1i_line)
451 return NULL;
452
453 return &e1i_line->ts[ts_nr-1];
454}
455
456struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
457{
458 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
459
460 if (!e1i_ts)
461 return NULL;
462
463 return &e1i_ts->trau.mux;
464}
465
466/* Signalling Link */
467
468struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
469 uint8_t tei, uint8_t sapi)
470{
471 struct e1inp_sign_link *link;
472
473 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
474 if (link->sapi == sapi && link->tei == tei)
475 return link;
476 }
477
478 return NULL;
479}
480
481/* create a new signalling link in a E1 timeslot */
482
483struct e1inp_sign_link *
484e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
485 struct gsm_bts_trx *trx, uint8_t tei,
486 uint8_t sapi)
487{
488 struct e1inp_sign_link *link;
489
490 if (ts->type != E1INP_TS_TYPE_SIGN)
491 return NULL;
492
493 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
494 if (!link)
495 return NULL;
496
497 link->ts = ts;
498 link->type = type;
499 INIT_LLIST_HEAD(&link->tx_list);
500 link->trx = trx;
501 link->tei = tei;
502 link->sapi = sapi;
503
504 llist_add_tail(&link->list, &ts->sign.sign_links);
505
506 return link;
507}
508
509void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
510{
511 struct msgb *msg;
512
513 llist_del(&link->list);
514 while (!llist_empty(&link->tx_list)) {
515 msg = msgb_dequeue(&link->tx_list);
516 msgb_free(msg);
517 }
518
519 if (link->ts->type == E1INP_TS_TYPE_SIGN)
520 osmo_timer_del(&link->ts->sign.tx_timer);
521
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200522 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200523 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200524
Pablo Neira Ayuso6cc3f922012-08-22 13:57:58 +0200525 e1inp_line_put(link->ts->line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200526 talloc_free(link);
527}
528
529/* XXX */
530/* the E1 driver tells us he has received something on a TS */
531int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
532 uint8_t tei, uint8_t sapi)
533{
534 struct e1inp_sign_link *link;
535 int ret = 0;
536
537 switch (ts->type) {
538 case E1INP_TS_TYPE_SIGN:
Harald Welte7f9d8512016-07-04 09:59:46 +0200539 /* we only need to write a 'Fake LAPD' packet here, if
540 * the underlying driver hides LAPD from us. If we use
541 * the libosmocore LAPD implementation, it will take
542 * care of writing the _actual_ LAPD packet */
543 if (!ts->lapd)
544 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200545 /* consult the list of signalling links */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200546 link = e1inp_lookup_sign_link(ts, tei, sapi);
547 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200548 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200549 "tei %d, sapi %d\n", tei, sapi);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200550 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200551 return -EINVAL;
552 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200553 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200554 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200555 "no action set for signalling messages.\n");
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200556 msgb_free(msg);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200557 return -ENOENT;
558 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200559 msg->dst = link;
560 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200561 break;
562 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200563 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200564 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200565 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200566 case E1INP_TS_TYPE_RAW:
567 ts->raw.recv_cb(ts, msg);
568 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200569 case E1INP_TS_TYPE_HDLC:
570 ts->hdlc.recv_cb(ts, msg);
571 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200572 default:
573 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200574 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200575 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200576 break;
577 }
578
579 return ret;
580}
581
Harald Weltefd44a5f2011-08-21 00:48:54 +0200582/*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot
583 * \param[in] e1i_ts E1 Timeslot data structure
584 * \param[in] msg Message buffer containing full LAPD message
585 *
586 * This is a wrapper around e1inp_rx_ts(), but feeding the incoming
587 * message first into our LAPD code. This allows a driver to read raw
588 * (HDLC decoded) data from the timeslot, instead of a LAPD stack
589 * present in any underlying driver.
590 */
591int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg)
592{
Harald Weltefd44a5f2011-08-21 00:48:54 +0200593 unsigned int sapi, tei;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200594 int ret = 0, error = 0;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200595
596 sapi = msg->data[0] >> 2;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200597 if ((msg->data[0] & 0x1))
598 tei = 0;
599 else
600 tei = msg->data[1] >> 1;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200601
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200602 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200603
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200604 ret = lapd_receive(e1i_ts->lapd, msg, &error);
605 if (ret < 0) {
Harald Weltefd44a5f2011-08-21 00:48:54 +0200606 switch(error) {
607 case LAPD_ERR_UNKNOWN_TEI:
608 /* We don't know about this TEI, probably the BSC
609 * lost local states (it crashed or it was stopped),
610 * notify the driver to see if it can do anything to
611 * recover the existing signalling links with the BTS.
612 */
613 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
614 return -EIO;
615 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200616 }
617
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200618 return 0;
619}
Harald Weltefd44a5f2011-08-21 00:48:54 +0200620
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200621void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi,
622 void *rx_cbdata)
623{
624 struct e1inp_ts *e1i_ts = rx_cbdata;
625 struct msgb *msg = dp->oph.msg;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200626
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200627 switch (dp->oph.primitive) {
628 case PRIM_DL_EST:
629 DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei);
630 e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200631 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200632 case PRIM_DL_REL:
633 DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei);
634 e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200635 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200636 case PRIM_DL_DATA:
637 case PRIM_DL_UNIT_DATA:
638 if (dp->oph.operation == PRIM_OP_INDICATION) {
639 msg->l2h = msg->l3h;
640 DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n",
641 osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)),
642 sapi, tei);
643 e1inp_rx_ts(e1i_ts, msg, tei, sapi);
644 return;
645 }
Harald Weltefd44a5f2011-08-21 00:48:54 +0200646 break;
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200647 case PRIM_MDL_ERROR:
648 DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200649 break;
650 default:
651 printf("ERROR: unknown prim\n");
652 break;
653 }
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200654
655 msgb_free(msg);
656
657 return;
Harald Weltefd44a5f2011-08-21 00:48:54 +0200658}
659
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200660#define TSX_ALLOC_SIZE 4096
661
662/* called by driver if it wants to transmit on a given TS */
663struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
664 struct e1inp_sign_link **sign_link)
665{
666 struct e1inp_sign_link *link;
667 struct msgb *msg = NULL;
668 int len;
669
670 switch (e1i_ts->type) {
671 case E1INP_TS_TYPE_SIGN:
672 /* FIXME: implement this round robin */
673 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
674 msg = msgb_dequeue(&link->tx_list);
675 if (msg) {
676 if (sign_link)
677 *sign_link = link;
678 break;
679 }
680 }
681 break;
682 case E1INP_TS_TYPE_TRAU:
683 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
684 if (!msg)
685 return NULL;
686 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
Pablo Neira Ayuso33f71752013-07-05 15:44:12 +0200687 if (len != 40) {
688 LOGP(DLMI, LOGL_ERROR,
689 "cannot transmit, failed to mux\n");
690 msgb_free(msg);
691 return NULL;
692 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200693 msgb_put(msg, 40);
694 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200695 case E1INP_TS_TYPE_RAW:
696 /* Get msgb from tx_queue */
697 msg = msgb_dequeue(&e1i_ts->raw.tx_queue);
698 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200699 case E1INP_TS_TYPE_HDLC:
700 /* Get msgb from tx_queue */
701 msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue);
702 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200703 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200704 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200705 return NULL;
706 }
707 return msg;
708}
709
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100710static int e1inp_int_snd_event(struct e1inp_ts *ts,
711 struct e1inp_sign_link *link, int evt)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200712{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200713 struct input_signal_data isd;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200714 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200715 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200716 isd.link_type = link->type;
717 isd.trx = link->trx;
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100718 isd.tei = link->tei;
719 isd.sapi = link->sapi;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200720
721 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200722 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200723 return 0;
724}
725
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100726
727/* called by driver in case some kind of link state event */
728int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
729{
730 struct e1inp_sign_link *link;
731
732 link = e1inp_lookup_sign_link(ts, tei, sapi);
733 if (!link)
734 return -EINVAL;
735 return e1inp_int_snd_event(ts, link, evt);
736}
737
738void e1inp_close_socket(struct e1inp_ts *ts,
739 struct e1inp_sign_link *sign_link,
740 struct osmo_fd *bfd)
741{
742 e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN);
743 /* the first e1inp_sign_link_destroy call closes the socket. */
744 if (bfd->fd != -1) {
745 osmo_fd_unregister(bfd);
746 close(bfd->fd);
747 bfd->fd = -1;
748 }
749}
750
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200751/* register a driver with the E1 core */
752int e1inp_driver_register(struct e1inp_driver *drv)
753{
754 llist_add_tail(&drv->list, &e1inp_driver_list);
755 return 0;
756}
757
758struct e1inp_driver *e1inp_driver_find(const char *name)
759{
760 struct e1inp_driver *drv;
761
762 llist_for_each_entry(drv, &e1inp_driver_list, list) {
763 if (!strcasecmp(name, drv->name))
764 return drv;
765 }
766 return NULL;
767}
768
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200769int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200770{
771 struct input_signal_data isd;
Harald Welte7f9d8512016-07-04 09:59:46 +0200772 int i, rc;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200773
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200774 e1inp_line_get(line);
775
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200776 if (line->driver && line->ops && line->driver->line_update) {
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200777 rc = line->driver->line_update(line);
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200778 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200779 rc = 0;
780
Harald Welte7f9d8512016-07-04 09:59:46 +0200781 /* Set the PCAP file descriptor for all timeslots that have
782 * software LAPD instances, to ensure the osmo_lapd_pcap code is
783 * used to write PCAP files (if requested) */
784 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
785 struct e1inp_ts *e1i_ts = &line->ts[i];
786 if (e1i_ts->lapd)
787 e1i_ts->lapd->pcap_fd = pcap_fd;
788 }
789
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200790 /* Send a signal to anyone who is interested in new lines being
791 * configured */
792 memset(&isd, 0, sizeof(isd));
793 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200794 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200795
796 return rc;
797}
798
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200799static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200800 void *handler_data, void *signal_data)
801{
Harald Weltecc2241b2011-07-19 16:06:06 +0200802 if (subsys != SS_L_GLOBAL ||
803 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200804 return 0;
805
806 if (pcap_fd) {
807 close(pcap_fd);
808 pcap_fd = -1;
809 }
810
811 return 0;
812}
813
814void e1inp_misdn_init(void);
815void e1inp_dahdi_init(void);
816void e1inp_ipaccess_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200817void e1inp_rs232_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200818
819void e1inp_init(void)
820{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200821 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200822 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
823 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200824 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200825
826 e1inp_misdn_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200827#ifdef HAVE_DAHDI_USER_H
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200828 e1inp_dahdi_init();
Harald Welte3bc78852011-08-24 08:32:38 +0200829#endif
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200830 e1inp_ipaccess_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200831 e1inp_rs232_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200832}