blob: eab418a248008f7e95881e00d4b80cf044dda340 [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] = {
Harald Welte4e254e32011-08-11 12:54:07 +020072 "hdlc.abort", "HDLC abort"
Harald Welte2f55f152011-08-11 12:50:13 +020073 },
74 [E1I_CTR_HDLC_BADFCS] = {
Harald Welte4e254e32011-08-11 12:54:07 +020075 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Welte2f55f152011-08-11 12:50:13 +020076 },
77 [E1I_CTR_HDLC_OVERR] = {
78 "hdlc.overrun", "HDLC Overrun"
79 },
80 [E1I_CTR_ALARM] = {
Harald Welte4e254e32011-08-11 12:54:07 +020081 "alarm", "Alarm"
Harald Welte2f55f152011-08-11 12:50:13 +020082 },
83 [E1I_CTR_REMOVED] = {
Harald Welte4e254e32011-08-11 12:54:07 +020084 "removed", "Line removed"
Harald Welte2f55f152011-08-11 12:50:13 +020085 },
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
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200249int abis_sendmsg(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800250{
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200251 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte59b04682009-06-10 05:40:52 +0800252 struct e1inp_driver *e1inp_driver;
253 struct e1inp_ts *e1i_ts;
254
255 msg->l2h = msg->data;
256
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200257 /* don't know how to route this message. */
258 if (sign_link == NULL) {
259 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200260 osmo_hexdump(msg->data, msg->len));
Harald Welteed831842009-06-27 03:09:08 +0200261 talloc_free(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800262 return -EINVAL;
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200263 }
264 e1i_ts = sign_link->ts;
265 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
266 /* notify the driver we have something to write */
267 e1inp_driver = sign_link->ts->line->driver;
268 e1inp_driver->want_write(e1i_ts);
269 }
270 msgb_enqueue(&sign_link->tx_list, msg);
271
272 /* dump it */
273 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
274
275 return 0;
276}
277
278int abis_rsl_sendmsg(struct msgb *msg)
279{
280 struct e1inp_sign_link *sign_link = (struct e1inp_sign_link *)msg->dst;
281 struct e1inp_driver *e1inp_driver;
282 struct e1inp_ts *e1i_ts;
283
284 msg->l2h = msg->data;
285
286 if (!sign_link) {
287 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200288 osmo_hexdump(msg->data, msg->len));
Harald Welte (local)42351bf2009-12-28 17:05:43 +0100289 talloc_free(msg);
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200290 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +0800291 }
Harald Welte59b04682009-06-10 05:40:52 +0800292 e1i_ts = sign_link->ts;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200293 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
Harald Welte59b04682009-06-10 05:40:52 +0800294 /* notify the driver we have something to write */
295 e1inp_driver = sign_link->ts->line->driver;
296 e1inp_driver->want_write(e1i_ts);
297 }
298 msgb_enqueue(&sign_link->tx_list, msg);
299
300 /* dump it */
301 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
302
303 return 0;
304}
305
Harald Weltec5845042011-02-14 15:26:13 +0100306int _abis_nm_sendmsg(struct msgb *msg, int to_trx_oml)
Harald Welte59b04682009-06-10 05:40:52 +0800307{
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200308 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte59b04682009-06-10 05:40:52 +0800309
310 msg->l2h = msg->data;
311
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200312 if (!msg->dst) {
313 LOGP(DNM, LOGL_ERROR, "_abis_nm_sendmsg: msg->dst == NULL\n");
Harald Welte59b04682009-06-10 05:40:52 +0800314 return -EINVAL;
315 }
316
Harald Weltecaeb9bc2011-02-13 19:36:18 +0100317 /* Check for TRX-specific OML link first */
Harald Weltec5845042011-02-14 15:26:13 +0100318 if (to_trx_oml) {
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200319 if (!sign_link->trx->oml_link)
Harald Weltec5845042011-02-14 15:26:13 +0100320 return -ENODEV;
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200321 msg->dst = sign_link->trx->oml_link;
Harald Welte59b04682009-06-10 05:40:52 +0800322 }
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200323 return abis_sendmsg(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800324}
325
326/* Timeslot */
327
328/* configure and initialize one e1inp_ts */
329int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
330 enum e1inp_ts_type type)
331{
Harald Welte62868882009-08-08 16:12:58 +0200332 if (ts->type == type && ts->line && line)
333 return 0;
334
Harald Welte59b04682009-06-10 05:40:52 +0800335 ts->type = type;
336 ts->line = line;
337
338 switch (type) {
339 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther5f833822011-01-16 18:12:13 +0100340 if (line && line->driver)
Holger Hans Peter Freyther038834b2010-04-26 16:02:04 +0800341 ts->sign.delay = line->driver->default_delay;
342 else
343 ts->sign.delay = 100000;
Harald Welte59b04682009-06-10 05:40:52 +0800344 INIT_LLIST_HEAD(&ts->sign.sign_links);
345 break;
346 case E1INP_TS_TYPE_TRAU:
347 subchan_mux_init(&ts->trau.mux);
348 ts->trau.demux.out_cb = subch_cb;
349 ts->trau.demux.data = ts;
350 subch_demux_init(&ts->trau.demux);
351 break;
352 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100353 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800354 ts->type);
355 return -EINVAL;
356 }
357 return 0;
358}
359
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200360struct e1inp_line *e1inp_line_get(uint8_t e1_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800361{
362 struct e1inp_line *e1i_line;
363
364 /* iterate over global list of e1 lines */
365 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
366 if (e1i_line->num == e1_nr)
367 return e1i_line;
368 }
369 return NULL;
370}
371
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200372struct e1inp_line *e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Harald Weltee76bea02011-02-05 13:54:41 +0100373{
374 struct e1inp_driver *driver;
375 struct e1inp_line *line;
376 int i;
377
378 line = e1inp_line_get(e1_nr);
Harald Weltecc5e9f82011-02-05 15:57:42 +0100379 if (line) {
380 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
381 e1_nr);
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 driver = e1inp_driver_find(driver_name);
Harald Weltecc5e9f82011-02-05 15:57:42 +0100386 if (!driver) {
387 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
388 driver_name);
Harald Weltee76bea02011-02-05 13:54:41 +0100389 return NULL;
Harald Weltecc5e9f82011-02-05 15:57:42 +0100390 }
Harald Weltee76bea02011-02-05 13:54:41 +0100391
392 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
393 if (!line)
394 return NULL;
395
396 line->driver = driver;
Harald Weltee76bea02011-02-05 13:54:41 +0100397 line->num = e1_nr;
Harald Welte2f55f152011-08-11 12:50:13 +0200398
399 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
400
Harald Weltee76bea02011-02-05 13:54:41 +0100401 for (i = 0; i < NUM_E1_TS; i++) {
402 line->ts[i].num = i+1;
403 line->ts[i].line = line;
404 }
405 llist_add_tail(&line->list, &e1inp_line_list);
406
407 return line;
408}
409
410#if 0
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200411struct e1inp_line *e1inp_line_get_create(uint8_t e1_nr)
Harald Welte62868882009-08-08 16:12:58 +0200412{
413 struct e1inp_line *line;
414 int i;
415
416 line = e1inp_line_get(e1_nr);
417 if (line)
418 return line;
419
420 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
421 if (!line)
422 return NULL;
423
424 line->num = e1_nr;
425 for (i = 0; i < NUM_E1_TS; i++) {
426 line->ts[i].num = i+1;
427 line->ts[i].line = line;
428 }
429 llist_add_tail(&line->list, &e1inp_line_list);
430
431 return line;
432}
Harald Weltee76bea02011-02-05 13:54:41 +0100433#endif
Harald Welte62868882009-08-08 16:12:58 +0200434
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200435static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800436{
437 struct e1inp_line *e1i_line;
438
439 e1i_line = e1inp_line_get(e1_nr);
440 if (!e1i_line)
441 return NULL;
442
443 return &e1i_line->ts[ts_nr-1];
444}
445
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200446struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800447{
448 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
449
450 if (!e1i_ts)
451 return NULL;
452
453 return &e1i_ts->trau.mux;
454}
455
456/* Signalling Link */
457
458struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200459 uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800460{
461 struct e1inp_sign_link *link;
462
463 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
464 if (link->sapi == sapi && link->tei == tei)
465 return link;
466 }
467
468 return NULL;
469}
470
471/* create a new signalling link in a E1 timeslot */
472
473struct e1inp_sign_link *
474e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200475 struct gsm_bts_trx *trx, uint8_t tei,
476 uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800477{
478 struct e1inp_sign_link *link;
479
480 if (ts->type != E1INP_TS_TYPE_SIGN)
481 return NULL;
482
Harald Welte857e00d2009-06-26 20:25:23 +0200483 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte59b04682009-06-10 05:40:52 +0800484 if (!link)
485 return NULL;
486
Harald Welte59b04682009-06-10 05:40:52 +0800487 link->ts = ts;
488 link->type = type;
489 INIT_LLIST_HEAD(&link->tx_list);
490 link->trx = trx;
491 link->tei = tei;
492 link->sapi = sapi;
493
494 llist_add_tail(&link->list, &ts->sign.sign_links);
495
496 return link;
497}
498
Harald Welte62868882009-08-08 16:12:58 +0200499void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
500{
Holger Hans Peter Freyther311a9932010-04-11 14:13:10 +0200501 struct msgb *msg;
502
Harald Welte62868882009-08-08 16:12:58 +0200503 llist_del(&link->list);
Holger Hans Peter Freyther311a9932010-04-11 14:13:10 +0200504 while (!llist_empty(&link->tx_list)) {
505 msg = msgb_dequeue(&link->tx_list);
506 msgb_free(msg);
507 }
508
Holger Hans Peter Freyther2da4f722010-04-11 17:18:02 +0200509 if (link->ts->type == E1INP_TS_TYPE_SIGN)
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200510 osmo_timer_del(&link->ts->sign.tx_timer);
Holger Hans Peter Freyther2da4f722010-04-11 17:18:02 +0200511
Harald Welte62868882009-08-08 16:12:58 +0200512 talloc_free(link);
513}
514
Harald Welte59b04682009-06-10 05:40:52 +0800515/* the E1 driver tells us he has received something on a TS */
516int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200517 uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800518{
519 struct e1inp_sign_link *link;
Harald Welte94809802011-02-12 12:29:21 +0100520 struct gsm_bts *bts;
Harald Welte59b04682009-06-10 05:40:52 +0800521 int ret;
522
523 switch (ts->type) {
524 case E1INP_TS_TYPE_SIGN:
525 /* consult the list of signalling links */
526 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
527 link = e1inp_lookup_sign_link(ts, tei, sapi);
528 if (!link) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100529 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte59b04682009-06-10 05:40:52 +0800530 "tei %d, sapi %d\n", tei, sapi);
531 return -EINVAL;
532 }
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100533
Harald Welte51d2a592010-03-26 21:28:59 +0800534 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte59b04682009-06-10 05:40:52 +0800535 switch (link->type) {
536 case E1INP_SIGN_OML:
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200537 msg->dst = link;
538 bts = link->trx->bts;
Harald Welte94809802011-02-12 12:29:21 +0100539 ret = bts->model->oml_rcvmsg(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800540 break;
541 case E1INP_SIGN_RSL:
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200542 msg->dst = link;
Harald Welte59b04682009-06-10 05:40:52 +0800543 ret = abis_rsl_rcvmsg(msg);
544 break;
545 default:
546 ret = -EINVAL;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100547 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte59b04682009-06-10 05:40:52 +0800548 break;
549 }
550 break;
551 case E1INP_TS_TYPE_TRAU:
552 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
553 break;
554 default:
555 ret = -EINVAL;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100556 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte59b04682009-06-10 05:40:52 +0800557 break;
558 }
559
560 return ret;
561}
562
563#define TSX_ALLOC_SIZE 4096
564
565/* called by driver if it wants to transmit on a given TS */
566struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
567 struct e1inp_sign_link **sign_link)
568{
569 struct e1inp_sign_link *link;
570 struct msgb *msg = NULL;
571 int len;
572
573 switch (e1i_ts->type) {
574 case E1INP_TS_TYPE_SIGN:
575 /* FIXME: implement this round robin */
576 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
577 msg = msgb_dequeue(&link->tx_list);
578 if (msg) {
579 if (sign_link)
580 *sign_link = link;
581 break;
582 }
583 }
584 break;
585 case E1INP_TS_TYPE_TRAU:
Harald Welte9cfc9352009-06-26 19:39:35 +0200586 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte59b04682009-06-10 05:40:52 +0800587 if (!msg)
588 return NULL;
589 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
590 msgb_put(msg, 40);
591 break;
592 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100593 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte59b04682009-06-10 05:40:52 +0800594 return NULL;
595 }
596 return msg;
597}
598
599/* called by driver in case some kind of link state event */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200600int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800601{
602 struct e1inp_sign_link *link;
Harald Welte4c826f72011-01-14 15:55:42 +0100603 struct input_signal_data isd;
Harald Welte59b04682009-06-10 05:40:52 +0800604
605 link = e1inp_lookup_sign_link(ts, tei, sapi);
606 if (!link)
607 return -EINVAL;
608
Pablo Neira Ayuso3f663282011-08-09 23:15:38 +0200609 isd.line = ts->line;
Harald Welte4c826f72011-01-14 15:55:42 +0100610 isd.link_type = link->type;
611 isd.trx = link->trx;
Harald Welte586d7702011-02-13 19:44:21 +0100612 isd.tei = tei;
613 isd.sapi = sapi;
Harald Welte4c826f72011-01-14 15:55:42 +0100614
615 /* report further upwards */
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200616 osmo_signal_dispatch(SS_INPUT, evt, &isd);
Harald Welte59b04682009-06-10 05:40:52 +0800617 return 0;
618}
619
620/* register a driver with the E1 core */
621int e1inp_driver_register(struct e1inp_driver *drv)
622{
623 llist_add_tail(&drv->list, &e1inp_driver_list);
624 return 0;
625}
626
Harald Weltee76bea02011-02-05 13:54:41 +0100627struct e1inp_driver *e1inp_driver_find(const char *name)
628{
629 struct e1inp_driver *drv;
630
Harald Weltee76bea02011-02-05 13:54:41 +0100631 llist_for_each_entry(drv, &e1inp_driver_list, list) {
632 if (!strcasecmp(name, drv->name))
633 return drv;
634 }
635 return NULL;
636}
637
Harald Welte62868882009-08-08 16:12:58 +0200638int e1inp_line_update(struct e1inp_line *line)
Harald Welte59b04682009-06-10 05:40:52 +0800639{
Harald Welte18e489a2011-02-11 16:49:41 +0100640 struct input_signal_data isd;
641 int rc;
642
Harald Welte760d4712011-02-05 12:26:55 +0100643 if (line->driver && line->driver->line_update)
Harald Welte18e489a2011-02-11 16:49:41 +0100644 rc = line->driver->line_update(line);
Harald Welte760d4712011-02-05 12:26:55 +0100645 else
Harald Welte18e489a2011-02-11 16:49:41 +0100646 rc = 0;
647
648 /* Send a signal to anyone who is interested in new lines being
649 * configured */
650 memset(&isd, 0, sizeof(isd));
651 isd.line = line;
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200652 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_INIT, &isd);
Harald Welte18e489a2011-02-11 16:49:41 +0100653
654 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800655}
Harald Welte932e20d2009-07-28 00:41:45 +0200656
Harald Welte4d42bd82009-11-30 19:43:54 +0100657static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
658 void *handler_data, void *signal_data)
659{
660 if (subsys != SS_GLOBAL ||
661 signal != S_GLOBAL_SHUTDOWN)
662 return 0;
663
664 if (pcap_fd) {
665 close(pcap_fd);
666 pcap_fd = -1;
667 }
668
669 return 0;
670}
671
Harald Weltee76bea02011-02-05 13:54:41 +0100672void e1inp_misdn_init(void);
Harald Welte890fe4a2011-02-05 13:58:46 +0100673void e1inp_dahdi_init(void);
Pablo Neira Ayuso50da93b2011-05-14 11:32:47 +0200674void e1inp_ipaccess_init(void);
675void e1inp_hsl_init(void);
Harald Weltee76bea02011-02-05 13:54:41 +0100676
677void e1inp_init(void)
Harald Welte932e20d2009-07-28 00:41:45 +0200678{
679 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
680 "e1inp_sign_link");
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200681 osmo_signal_register_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Weltee76bea02011-02-05 13:54:41 +0100682
683 e1inp_misdn_init();
Harald Weltec7841cc2011-02-05 15:13:27 +0100684#ifdef HAVE_DAHDI_USER_H
Harald Welte890fe4a2011-02-05 13:58:46 +0100685 e1inp_dahdi_init();
Harald Weltec7841cc2011-02-05 15:13:27 +0100686#endif
Pablo Neira Ayuso50da93b2011-05-14 11:32:47 +0200687 e1inp_ipaccess_init();
688 e1inp_hsl_init();
Harald Welte932e20d2009-07-28 00:41:45 +0200689}