blob: f73862673e767aa3856bad679dde4277c7610620 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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
Harald Welte0e3e88e2011-01-01 15:25:50 +01008 * 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
Harald Welte59b04682009-06-10 05:40:52 +080010 * (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
Harald Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080016 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * 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/>.
Harald Welte59b04682009-06-10 05:40:52 +080019 *
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <stdlib.h>
25#include <errno.h>
26#include <string.h>
27#include <time.h>
28#include <sys/fcntl.h>
Harald Welte59b04682009-06-10 05:40:52 +080029#include <sys/socket.h>
30#include <sys/ioctl.h>
31#include <arpa/inet.h>
32#include <mISDNif.h>
33
34//#define AF_COMPATIBILITY_FUNC
35//#include <compat_af_isdn.h>
36#ifndef AF_ISDN
37#define AF_ISDN 34
38#define PF_ISDN AF_ISDN
39#endif
40
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010041#include <osmocom/core/select.h>
Harald Welte2f55f152011-08-11 12:50:13 +020042#include <osmocom/core/linuxlist.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010043#include <osmocom/core/msgb.h>
Harald Welte2f55f152011-08-11 12:50:13 +020044#include <osmocom/core/talloc.h>
45#include <osmocom/core/rate_ctr.h>
46
Harald Welte59b04682009-06-10 05:40:52 +080047#include <openbsc/debug.h>
48#include <openbsc/gsm_data.h>
49#include <openbsc/e1_input.h>
50#include <openbsc/abis_nm.h>
51#include <openbsc/abis_rsl.h>
Harald Welte59b04682009-06-10 05:40:52 +080052#include <openbsc/subchan_demux.h>
53#include <openbsc/trau_frame.h>
54#include <openbsc/trau_mux.h>
Harald Welte4d42bd82009-11-30 19:43:54 +010055#include <openbsc/signal.h>
Holger Hans Peter Freyther735cd9e2009-08-10 07:54:02 +020056#include <openbsc/misdn.h>
Harald Welte59b04682009-06-10 05:40:52 +080057
Harald Welted9dea592011-03-04 13:53:51 +010058#include "../../bscconfig.h"
Harald Weltec7841cc2011-02-05 15:13:27 +010059
Harald Welte59b04682009-06-10 05:40:52 +080060#define NUM_E1_TS 32
61
62/* list of all E1 drivers */
63LLIST_HEAD(e1inp_driver_list);
64
65/* list of all E1 lines */
66LLIST_HEAD(e1inp_line_list);
67
Harald Weltea8379772009-06-20 22:36:41 +020068static void *tall_sigl_ctx;
69
Harald Welte2f55f152011-08-11 12:50:13 +020070static const struct rate_ctr_desc e1inp_ctr_d[] = {
71 [E1I_CTR_HDLC_ABORT] = {
72 "hdlc.abort", "ABORT from E1 Layer1"
73 },
74 [E1I_CTR_HDLC_BADFCS] = {
75 "hdlc.bad_fcs", "Bad Frame Check Sequence"
76 },
77 [E1I_CTR_HDLC_OVERR] = {
78 "hdlc.overrun", "HDLC Overrun"
79 },
80 [E1I_CTR_ALARM] = {
81 "alarm", "E1 Alarm (Yellow/Red)"
82 },
83 [E1I_CTR_REMOVED] = {
84 "removed", "E1 Line removed"
85 },
86};
87
88static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
89 .group_name_prefix = "e1inp",
90 .group_description = "E1 Input subsystem",
91 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
92 .ctr_desc = e1inp_ctr_d,
93};
94
Harald Welte59b04682009-06-10 05:40:52 +080095/*
96 * pcap writing of the misdn load
97 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
98 */
99#define DLT_LINUX_LAPD 177
100#define PCAP_INPUT 0
101#define PCAP_OUTPUT 1
102
103struct pcap_hdr {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200104 uint32_t magic_number;
105 uint16_t version_major;
106 uint16_t version_minor;
Harald Welte59b04682009-06-10 05:40:52 +0800107 int32_t thiszone;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200108 uint32_t sigfigs;
109 uint32_t snaplen;
110 uint32_t network;
Harald Welte59b04682009-06-10 05:40:52 +0800111} __attribute__((packed));
112
113struct pcaprec_hdr {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200114 uint32_t ts_sec;
115 uint32_t ts_usec;
116 uint32_t incl_len;
117 uint32_t orig_len;
Harald Welte59b04682009-06-10 05:40:52 +0800118} __attribute__((packed));
119
120struct fake_linux_lapd_header {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200121 uint16_t pkttype;
122 uint16_t hatype;
123 uint16_t halen;
124 uint64_t addr;
Harald Welte59b04682009-06-10 05:40:52 +0800125 int16_t protocol;
126} __attribute__((packed));
127
128struct lapd_header {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200129 uint8_t ea1 : 1;
130 uint8_t cr : 1;
131 uint8_t sapi : 6;
132 uint8_t ea2 : 1;
133 uint8_t tei : 7;
134 uint8_t control_foo; /* fake UM's ... */
Harald Welte59b04682009-06-10 05:40:52 +0800135} __attribute__((packed));
136
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200137osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
138osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
139osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
140osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
141osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
Harald Welte59b04682009-06-10 05:40:52 +0800142
143
144static int pcap_fd = -1;
145
146void e1_set_pcap_fd(int fd)
147{
148 int ret;
149 struct pcap_hdr header = {
150 .magic_number = 0xa1b2c3d4,
151 .version_major = 2,
152 .version_minor = 4,
153 .thiszone = 0,
154 .sigfigs = 0,
155 .snaplen = 65535,
156 .network = DLT_LINUX_LAPD,
157 };
158
159 pcap_fd = fd;
160 ret = write(pcap_fd, &header, sizeof(header));
161}
162
163/* This currently only works for the D-Channel */
164static void write_pcap_packet(int direction, int sapi, int tei,
165 struct msgb *msg) {
166 if (pcap_fd < 0)
167 return;
168
169 int ret;
170 time_t cur_time;
171 struct tm *tm;
172
173 struct fake_linux_lapd_header header = {
174 .pkttype = 4,
175 .hatype = 0,
176 .halen = 0,
177 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
178 .protocol = ntohs(48),
179 };
180
181 struct lapd_header lapd_header = {
182 .ea1 = 0,
183 .cr = direction == PCAP_OUTPUT ? 1 : 0,
184 .sapi = sapi & 0x3F,
185 .ea2 = 1,
186 .tei = tei & 0x7F,
187 .control_foo = 0x03 /* UI */,
188 };
189
190 struct pcaprec_hdr payload_header = {
191 .ts_sec = 0,
192 .ts_usec = 0,
Harald Welte81c51a82011-02-12 14:00:23 +0100193 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
194 + sizeof(struct lapd_header),
195 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
196 + sizeof(struct lapd_header),
Harald Welte59b04682009-06-10 05:40:52 +0800197 };
198
199
200 cur_time = time(NULL);
201 tm = localtime(&cur_time);
202 payload_header.ts_sec = mktime(tm);
203
204 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
205 ret = write(pcap_fd, &header, sizeof(header));
206 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Harald Welte81c51a82011-02-12 14:00:23 +0100207 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
Harald Welte59b04682009-06-10 05:40:52 +0800208}
209
210static const char *sign_types[] = {
211 [E1INP_SIGN_NONE] = "None",
212 [E1INP_SIGN_OML] = "OML",
213 [E1INP_SIGN_RSL] = "RSL",
214};
215const char *e1inp_signtype_name(enum e1inp_sign_type tp)
216{
217 if (tp >= ARRAY_SIZE(sign_types))
218 return "undefined";
219 return sign_types[tp];
220}
221
222static const char *ts_types[] = {
223 [E1INP_TS_TYPE_NONE] = "None",
224 [E1INP_TS_TYPE_SIGN] = "Signalling",
225 [E1INP_TS_TYPE_TRAU] = "TRAU",
226};
227
228const char *e1inp_tstype_name(enum e1inp_ts_type tp)
229{
230 if (tp >= ARRAY_SIZE(ts_types))
231 return "undefined";
232 return ts_types[tp];
233}
234
235/* callback when a TRAU frame was received */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200236static int subch_cb(struct subch_demux *dmx, int ch, uint8_t *data, int len,
Harald Welte59b04682009-06-10 05:40:52 +0800237 void *_priv)
238{
239 struct e1inp_ts *e1i_ts = _priv;
240 struct gsm_e1_subslot src_ss;
241
242 src_ss.e1_nr = e1i_ts->line->num;
243 src_ss.e1_ts = e1i_ts->num;
244 src_ss.e1_ts_ss = ch;
245
246 return trau_mux_input(&src_ss, data, len);
247}
248
249int abis_rsl_sendmsg(struct msgb *msg)
250{
251 struct e1inp_sign_link *sign_link;
252 struct e1inp_driver *e1inp_driver;
253 struct e1inp_ts *e1i_ts;
254
255 msg->l2h = msg->data;
256
Harald Welte (local)42351bf2009-12-28 17:05:43 +0100257 if (!msg->trx) {
258 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200259 osmo_hexdump(msg->data, msg->len));
Harald Welteed831842009-06-27 03:09:08 +0200260 talloc_free(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800261 return -EINVAL;
Harald Welte (local)42351bf2009-12-28 17:05:43 +0100262 } else if (!msg->trx->rsl_link) {
263 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200264 osmo_hexdump(msg->data, msg->len));
Harald Welte (local)42351bf2009-12-28 17:05:43 +0100265 talloc_free(msg);
266 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +0800267 }
268
269 sign_link = msg->trx->rsl_link;
270 e1i_ts = sign_link->ts;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200271 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
Harald Welte59b04682009-06-10 05:40:52 +0800272 /* notify the driver we have something to write */
273 e1inp_driver = sign_link->ts->line->driver;
274 e1inp_driver->want_write(e1i_ts);
275 }
276 msgb_enqueue(&sign_link->tx_list, msg);
277
278 /* dump it */
279 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
280
281 return 0;
282}
283
Harald Weltec5845042011-02-14 15:26:13 +0100284int _abis_nm_sendmsg(struct msgb *msg, int to_trx_oml)
Harald Welte59b04682009-06-10 05:40:52 +0800285{
286 struct e1inp_sign_link *sign_link;
287 struct e1inp_driver *e1inp_driver;
288 struct e1inp_ts *e1i_ts;
289
290 msg->l2h = msg->data;
291
292 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Harald Weltee053f3f2011-02-05 20:18:18 +0100293 LOGP(DNM, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte59b04682009-06-10 05:40:52 +0800294 return -EINVAL;
295 }
296
Harald Weltecaeb9bc2011-02-13 19:36:18 +0100297 /* Check for TRX-specific OML link first */
Harald Weltec5845042011-02-14 15:26:13 +0100298 if (to_trx_oml) {
299 if (!msg->trx->oml_link)
300 return -ENODEV;
Harald Weltecaeb9bc2011-02-13 19:36:18 +0100301 sign_link = msg->trx->oml_link;
Harald Weltec5845042011-02-14 15:26:13 +0100302 } else
Harald Weltecaeb9bc2011-02-13 19:36:18 +0100303 sign_link = msg->trx->bts->oml_link;
304
Harald Welte59b04682009-06-10 05:40:52 +0800305 e1i_ts = sign_link->ts;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200306 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
Harald Welte59b04682009-06-10 05:40:52 +0800307 /* notify the driver we have something to write */
308 e1inp_driver = sign_link->ts->line->driver;
309 e1inp_driver->want_write(e1i_ts);
310 }
311 msgb_enqueue(&sign_link->tx_list, msg);
312
313 /* dump it */
314 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
315
316 return 0;
317}
318
319/* Timeslot */
320
321/* configure and initialize one e1inp_ts */
322int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
323 enum e1inp_ts_type type)
324{
Harald Welte62868882009-08-08 16:12:58 +0200325 if (ts->type == type && ts->line && line)
326 return 0;
327
Harald Welte59b04682009-06-10 05:40:52 +0800328 ts->type = type;
329 ts->line = line;
330
331 switch (type) {
332 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther5f833822011-01-16 18:12:13 +0100333 if (line && line->driver)
Holger Hans Peter Freyther038834b2010-04-26 16:02:04 +0800334 ts->sign.delay = line->driver->default_delay;
335 else
336 ts->sign.delay = 100000;
Harald Welte59b04682009-06-10 05:40:52 +0800337 INIT_LLIST_HEAD(&ts->sign.sign_links);
338 break;
339 case E1INP_TS_TYPE_TRAU:
340 subchan_mux_init(&ts->trau.mux);
341 ts->trau.demux.out_cb = subch_cb;
342 ts->trau.demux.data = ts;
343 subch_demux_init(&ts->trau.demux);
344 break;
345 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100346 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800347 ts->type);
348 return -EINVAL;
349 }
350 return 0;
351}
352
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200353struct e1inp_line *e1inp_line_get(uint8_t e1_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800354{
355 struct e1inp_line *e1i_line;
356
357 /* iterate over global list of e1 lines */
358 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
359 if (e1i_line->num == e1_nr)
360 return e1i_line;
361 }
362 return NULL;
363}
364
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200365struct e1inp_line *e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Harald Weltee76bea02011-02-05 13:54:41 +0100366{
367 struct e1inp_driver *driver;
368 struct e1inp_line *line;
369 int i;
370
371 line = e1inp_line_get(e1_nr);
Harald Weltecc5e9f82011-02-05 15:57:42 +0100372 if (line) {
373 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
374 e1_nr);
Harald Weltee76bea02011-02-05 13:54:41 +0100375 return NULL;
Harald Weltecc5e9f82011-02-05 15:57:42 +0100376 }
Harald Weltee76bea02011-02-05 13:54:41 +0100377
378 driver = e1inp_driver_find(driver_name);
Harald Weltecc5e9f82011-02-05 15:57:42 +0100379 if (!driver) {
380 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
381 driver_name);
Harald Weltee76bea02011-02-05 13:54:41 +0100382 return NULL;
Harald Weltecc5e9f82011-02-05 15:57:42 +0100383 }
Harald Weltee76bea02011-02-05 13:54:41 +0100384
385 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
386 if (!line)
387 return NULL;
388
389 line->driver = driver;
Harald Weltee76bea02011-02-05 13:54:41 +0100390 line->num = e1_nr;
Harald Welte2f55f152011-08-11 12:50:13 +0200391
392 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
393
Harald Weltee76bea02011-02-05 13:54:41 +0100394 for (i = 0; i < NUM_E1_TS; i++) {
395 line->ts[i].num = i+1;
396 line->ts[i].line = line;
397 }
398 llist_add_tail(&line->list, &e1inp_line_list);
399
400 return line;
401}
402
403#if 0
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200404struct e1inp_line *e1inp_line_get_create(uint8_t e1_nr)
Harald Welte62868882009-08-08 16:12:58 +0200405{
406 struct e1inp_line *line;
407 int i;
408
409 line = e1inp_line_get(e1_nr);
410 if (line)
411 return line;
412
413 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
414 if (!line)
415 return NULL;
416
417 line->num = e1_nr;
418 for (i = 0; i < NUM_E1_TS; i++) {
419 line->ts[i].num = i+1;
420 line->ts[i].line = line;
421 }
422 llist_add_tail(&line->list, &e1inp_line_list);
423
424 return line;
425}
Harald Weltee76bea02011-02-05 13:54:41 +0100426#endif
Harald Welte62868882009-08-08 16:12:58 +0200427
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200428static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800429{
430 struct e1inp_line *e1i_line;
431
432 e1i_line = e1inp_line_get(e1_nr);
433 if (!e1i_line)
434 return NULL;
435
436 return &e1i_line->ts[ts_nr-1];
437}
438
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200439struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800440{
441 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
442
443 if (!e1i_ts)
444 return NULL;
445
446 return &e1i_ts->trau.mux;
447}
448
449/* Signalling Link */
450
451struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200452 uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800453{
454 struct e1inp_sign_link *link;
455
456 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
457 if (link->sapi == sapi && link->tei == tei)
458 return link;
459 }
460
461 return NULL;
462}
463
464/* create a new signalling link in a E1 timeslot */
465
466struct e1inp_sign_link *
467e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200468 struct gsm_bts_trx *trx, uint8_t tei,
469 uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800470{
471 struct e1inp_sign_link *link;
472
473 if (ts->type != E1INP_TS_TYPE_SIGN)
474 return NULL;
475
Harald Welte857e00d2009-06-26 20:25:23 +0200476 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte59b04682009-06-10 05:40:52 +0800477 if (!link)
478 return NULL;
479
Harald Welte59b04682009-06-10 05:40:52 +0800480 link->ts = ts;
481 link->type = type;
482 INIT_LLIST_HEAD(&link->tx_list);
483 link->trx = trx;
484 link->tei = tei;
485 link->sapi = sapi;
486
487 llist_add_tail(&link->list, &ts->sign.sign_links);
488
489 return link;
490}
491
Harald Welte62868882009-08-08 16:12:58 +0200492void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
493{
Holger Hans Peter Freyther311a9932010-04-11 14:13:10 +0200494 struct msgb *msg;
495
Harald Welte62868882009-08-08 16:12:58 +0200496 llist_del(&link->list);
Holger Hans Peter Freyther311a9932010-04-11 14:13:10 +0200497 while (!llist_empty(&link->tx_list)) {
498 msg = msgb_dequeue(&link->tx_list);
499 msgb_free(msg);
500 }
501
Holger Hans Peter Freyther2da4f722010-04-11 17:18:02 +0200502 if (link->ts->type == E1INP_TS_TYPE_SIGN)
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200503 osmo_timer_del(&link->ts->sign.tx_timer);
Holger Hans Peter Freyther2da4f722010-04-11 17:18:02 +0200504
Harald Welte62868882009-08-08 16:12:58 +0200505 talloc_free(link);
506}
507
Harald Welte59b04682009-06-10 05:40:52 +0800508/* the E1 driver tells us he has received something on a TS */
509int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200510 uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800511{
512 struct e1inp_sign_link *link;
Harald Welte94809802011-02-12 12:29:21 +0100513 struct gsm_bts *bts;
Harald Welte59b04682009-06-10 05:40:52 +0800514 int ret;
515
516 switch (ts->type) {
517 case E1INP_TS_TYPE_SIGN:
518 /* consult the list of signalling links */
519 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
520 link = e1inp_lookup_sign_link(ts, tei, sapi);
521 if (!link) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100522 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte59b04682009-06-10 05:40:52 +0800523 "tei %d, sapi %d\n", tei, sapi);
524 return -EINVAL;
525 }
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100526
Harald Welte51d2a592010-03-26 21:28:59 +0800527 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte59b04682009-06-10 05:40:52 +0800528 switch (link->type) {
529 case E1INP_SIGN_OML:
530 msg->trx = link->trx;
Harald Welte94809802011-02-12 12:29:21 +0100531 bts = msg->trx->bts;
532 ret = bts->model->oml_rcvmsg(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800533 break;
534 case E1INP_SIGN_RSL:
535 msg->trx = link->trx;
536 ret = abis_rsl_rcvmsg(msg);
537 break;
538 default:
539 ret = -EINVAL;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100540 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte59b04682009-06-10 05:40:52 +0800541 break;
542 }
543 break;
544 case E1INP_TS_TYPE_TRAU:
545 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
546 break;
547 default:
548 ret = -EINVAL;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100549 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte59b04682009-06-10 05:40:52 +0800550 break;
551 }
552
553 return ret;
554}
555
556#define TSX_ALLOC_SIZE 4096
557
558/* called by driver if it wants to transmit on a given TS */
559struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
560 struct e1inp_sign_link **sign_link)
561{
562 struct e1inp_sign_link *link;
563 struct msgb *msg = NULL;
564 int len;
565
566 switch (e1i_ts->type) {
567 case E1INP_TS_TYPE_SIGN:
568 /* FIXME: implement this round robin */
569 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
570 msg = msgb_dequeue(&link->tx_list);
571 if (msg) {
572 if (sign_link)
573 *sign_link = link;
574 break;
575 }
576 }
577 break;
578 case E1INP_TS_TYPE_TRAU:
Harald Welte9cfc9352009-06-26 19:39:35 +0200579 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte59b04682009-06-10 05:40:52 +0800580 if (!msg)
581 return NULL;
582 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
583 msgb_put(msg, 40);
584 break;
585 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100586 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte59b04682009-06-10 05:40:52 +0800587 return NULL;
588 }
589 return msg;
590}
591
592/* called by driver in case some kind of link state event */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200593int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800594{
595 struct e1inp_sign_link *link;
Harald Welte4c826f72011-01-14 15:55:42 +0100596 struct input_signal_data isd;
Harald Welte59b04682009-06-10 05:40:52 +0800597
598 link = e1inp_lookup_sign_link(ts, tei, sapi);
599 if (!link)
600 return -EINVAL;
601
Pablo Neira Ayuso3f663282011-08-09 23:15:38 +0200602 isd.line = ts->line;
Harald Welte4c826f72011-01-14 15:55:42 +0100603 isd.link_type = link->type;
604 isd.trx = link->trx;
Harald Welte586d7702011-02-13 19:44:21 +0100605 isd.tei = tei;
606 isd.sapi = sapi;
Harald Welte4c826f72011-01-14 15:55:42 +0100607
608 /* report further upwards */
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200609 osmo_signal_dispatch(SS_INPUT, evt, &isd);
Harald Welte59b04682009-06-10 05:40:52 +0800610 return 0;
611}
612
613/* register a driver with the E1 core */
614int e1inp_driver_register(struct e1inp_driver *drv)
615{
616 llist_add_tail(&drv->list, &e1inp_driver_list);
617 return 0;
618}
619
Harald Weltee76bea02011-02-05 13:54:41 +0100620struct e1inp_driver *e1inp_driver_find(const char *name)
621{
622 struct e1inp_driver *drv;
623
Harald Weltee76bea02011-02-05 13:54:41 +0100624 llist_for_each_entry(drv, &e1inp_driver_list, list) {
625 if (!strcasecmp(name, drv->name))
626 return drv;
627 }
628 return NULL;
629}
630
Harald Welte62868882009-08-08 16:12:58 +0200631int e1inp_line_update(struct e1inp_line *line)
Harald Welte59b04682009-06-10 05:40:52 +0800632{
Harald Welte18e489a2011-02-11 16:49:41 +0100633 struct input_signal_data isd;
634 int rc;
635
Harald Welte760d4712011-02-05 12:26:55 +0100636 if (line->driver && line->driver->line_update)
Harald Welte18e489a2011-02-11 16:49:41 +0100637 rc = line->driver->line_update(line);
Harald Welte760d4712011-02-05 12:26:55 +0100638 else
Harald Welte18e489a2011-02-11 16:49:41 +0100639 rc = 0;
640
641 /* Send a signal to anyone who is interested in new lines being
642 * configured */
643 memset(&isd, 0, sizeof(isd));
644 isd.line = line;
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200645 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_INIT, &isd);
Harald Welte18e489a2011-02-11 16:49:41 +0100646
647 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800648}
Harald Welte932e20d2009-07-28 00:41:45 +0200649
Harald Welte4d42bd82009-11-30 19:43:54 +0100650static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
651 void *handler_data, void *signal_data)
652{
653 if (subsys != SS_GLOBAL ||
654 signal != S_GLOBAL_SHUTDOWN)
655 return 0;
656
657 if (pcap_fd) {
658 close(pcap_fd);
659 pcap_fd = -1;
660 }
661
662 return 0;
663}
664
Harald Weltee76bea02011-02-05 13:54:41 +0100665void e1inp_misdn_init(void);
Harald Welte890fe4a2011-02-05 13:58:46 +0100666void e1inp_dahdi_init(void);
Pablo Neira Ayuso50da93b2011-05-14 11:32:47 +0200667void e1inp_ipaccess_init(void);
668void e1inp_hsl_init(void);
Harald Weltee76bea02011-02-05 13:54:41 +0100669
670void e1inp_init(void)
Harald Welte932e20d2009-07-28 00:41:45 +0200671{
672 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
673 "e1inp_sign_link");
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200674 osmo_signal_register_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Weltee76bea02011-02-05 13:54:41 +0100675
676 e1inp_misdn_init();
Harald Weltec7841cc2011-02-05 15:13:27 +0100677#ifdef HAVE_DAHDI_USER_H
Harald Welte890fe4a2011-02-05 13:58:46 +0100678 e1inp_dahdi_init();
Harald Weltec7841cc2011-02-05 15:13:27 +0100679#endif
Pablo Neira Ayuso50da93b2011-05-14 11:32:47 +0200680 e1inp_ipaccess_init();
681 e1inp_hsl_init();
Harald Welte932e20d2009-07-28 00:41:45 +0200682}