blob: 1a79261d46bed2d0f22729b5a2a91029304bd522 [file] [log] [blame]
Harald Welte1fa60c82009-02-09 18:13:26 +00001/* 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 Welte9af6ddf2011-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 Welte1fa60c82009-02-09 18:13:26 +000010 * (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 Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte1fa60c82009-02-09 18:13:26 +000016 *
Harald Welte9af6ddf2011-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 Welte1fa60c82009-02-09 18:13:26 +000019 *
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 Welte1fa60c82009-02-09 18:13:26 +000029#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>
Holger Freyther66e092b2009-03-31 12:13:50 +000036#ifndef AF_ISDN
Harald Welte1fa60c82009-02-09 18:13:26 +000037#define AF_ISDN 34
38#define PF_ISDN AF_ISDN
Holger Freyther66e092b2009-03-31 12:13:50 +000039#endif
Harald Welte1fa60c82009-02-09 18:13:26 +000040
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010041#include <osmocom/core/select.h>
42#include <osmocom/core/msgb.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000043#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
45#include <openbsc/e1_input.h>
46#include <openbsc/abis_nm.h>
47#include <openbsc/abis_rsl.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010048#include <osmocom/core/linuxlist.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000049#include <openbsc/subchan_demux.h>
Harald Welte45b407a2009-05-23 15:51:12 +000050#include <openbsc/trau_frame.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000051#include <openbsc/trau_mux.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/talloc.h>
Harald Welte29b9cf82009-11-30 19:43:54 +010053#include <openbsc/signal.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020054#include <openbsc/misdn.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000055
Harald Weltec08e8be2011-03-04 13:53:51 +010056#include "../../bscconfig.h"
Harald Welteca17ef82011-02-05 15:13:27 +010057
Harald Welte1fa60c82009-02-09 18:13:26 +000058#define NUM_E1_TS 32
59
60/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000061LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000062
63/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000064LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000065
Harald Welte2cf161b2009-06-20 22:36:41 +020066static void *tall_sigl_ctx;
67
Harald Welte1fa60c82009-02-09 18:13:26 +000068/*
69 * pcap writing of the misdn load
70 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
71 */
72#define DLT_LINUX_LAPD 177
73#define PCAP_INPUT 0
74#define PCAP_OUTPUT 1
75
76struct pcap_hdr {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077 uint32_t magic_number;
78 uint16_t version_major;
79 uint16_t version_minor;
Harald Welte1fa60c82009-02-09 18:13:26 +000080 int32_t thiszone;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020081 uint32_t sigfigs;
82 uint32_t snaplen;
83 uint32_t network;
Harald Welte1fa60c82009-02-09 18:13:26 +000084} __attribute__((packed));
85
86struct pcaprec_hdr {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020087 uint32_t ts_sec;
88 uint32_t ts_usec;
89 uint32_t incl_len;
90 uint32_t orig_len;
Harald Welte1fa60c82009-02-09 18:13:26 +000091} __attribute__((packed));
92
93struct fake_linux_lapd_header {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020094 uint16_t pkttype;
95 uint16_t hatype;
96 uint16_t halen;
97 uint64_t addr;
Harald Welte1fa60c82009-02-09 18:13:26 +000098 int16_t protocol;
99} __attribute__((packed));
100
101struct lapd_header {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200102 uint8_t ea1 : 1;
103 uint8_t cr : 1;
104 uint8_t sapi : 6;
105 uint8_t ea2 : 1;
106 uint8_t tei : 7;
107 uint8_t control_foo; /* fake UM's ... */
Harald Welte1fa60c82009-02-09 18:13:26 +0000108} __attribute__((packed));
109
Holger Hans Peter Freyther0b369c52010-11-09 23:10:16 +0100110static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
111static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
112static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
113static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
114static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
Harald Welte1fa60c82009-02-09 18:13:26 +0000115
116
117static int pcap_fd = -1;
118
Holger Freyther0469cf62009-03-31 12:14:16 +0000119void e1_set_pcap_fd(int fd)
Harald Welte1fa60c82009-02-09 18:13:26 +0000120{
121 int ret;
122 struct pcap_hdr header = {
123 .magic_number = 0xa1b2c3d4,
124 .version_major = 2,
125 .version_minor = 4,
126 .thiszone = 0,
127 .sigfigs = 0,
128 .snaplen = 65535,
129 .network = DLT_LINUX_LAPD,
130 };
131
132 pcap_fd = fd;
133 ret = write(pcap_fd, &header, sizeof(header));
134}
135
136/* This currently only works for the D-Channel */
Holger Freyther0469cf62009-03-31 12:14:16 +0000137static void write_pcap_packet(int direction, int sapi, int tei,
Harald Welte1fa60c82009-02-09 18:13:26 +0000138 struct msgb *msg) {
139 if (pcap_fd < 0)
140 return;
141
142 int ret;
143 time_t cur_time;
144 struct tm *tm;
145
146 struct fake_linux_lapd_header header = {
147 .pkttype = 4,
148 .hatype = 0,
149 .halen = 0,
150 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
151 .protocol = ntohs(48),
152 };
153
154 struct lapd_header lapd_header = {
155 .ea1 = 0,
156 .cr = direction == PCAP_OUTPUT ? 1 : 0,
Holger Freyther0469cf62009-03-31 12:14:16 +0000157 .sapi = sapi & 0x3F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000158 .ea2 = 1,
Holger Freyther0469cf62009-03-31 12:14:16 +0000159 .tei = tei & 0x7F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000160 .control_foo = 0x03 /* UI */,
161 };
162
163 struct pcaprec_hdr payload_header = {
164 .ts_sec = 0,
165 .ts_usec = 0,
Harald Welte95aa5c42011-02-12 14:00:23 +0100166 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
167 + sizeof(struct lapd_header),
168 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
169 + sizeof(struct lapd_header),
Harald Welte1fa60c82009-02-09 18:13:26 +0000170 };
171
172
173 cur_time = time(NULL);
174 tm = localtime(&cur_time);
175 payload_header.ts_sec = mktime(tm);
176
177 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
178 ret = write(pcap_fd, &header, sizeof(header));
179 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Harald Welte95aa5c42011-02-12 14:00:23 +0100180 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000181}
Harald Welte1fa60c82009-02-09 18:13:26 +0000182
Harald Welted256d4f2009-03-10 19:44:48 +0000183static const char *sign_types[] = {
184 [E1INP_SIGN_NONE] = "None",
185 [E1INP_SIGN_OML] = "OML",
186 [E1INP_SIGN_RSL] = "RSL",
187};
188const char *e1inp_signtype_name(enum e1inp_sign_type tp)
189{
190 if (tp >= ARRAY_SIZE(sign_types))
191 return "undefined";
192 return sign_types[tp];
193}
194
195static const char *ts_types[] = {
196 [E1INP_TS_TYPE_NONE] = "None",
197 [E1INP_TS_TYPE_SIGN] = "Signalling",
198 [E1INP_TS_TYPE_TRAU] = "TRAU",
199};
200
201const char *e1inp_tstype_name(enum e1inp_ts_type tp)
202{
203 if (tp >= ARRAY_SIZE(ts_types))
204 return "undefined";
205 return ts_types[tp];
206}
207
Harald Welte1fa60c82009-02-09 18:13:26 +0000208/* callback when a TRAU frame was received */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200209static int subch_cb(struct subch_demux *dmx, int ch, uint8_t *data, int len,
Harald Welte1fa60c82009-02-09 18:13:26 +0000210 void *_priv)
211{
212 struct e1inp_ts *e1i_ts = _priv;
213 struct gsm_e1_subslot src_ss;
214
215 src_ss.e1_nr = e1i_ts->line->num;
216 src_ss.e1_ts = e1i_ts->num;
217 src_ss.e1_ts_ss = ch;
218
219 return trau_mux_input(&src_ss, data, len);
220}
221
222int abis_rsl_sendmsg(struct msgb *msg)
223{
224 struct e1inp_sign_link *sign_link;
225 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000226 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000227
228 msg->l2h = msg->data;
229
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100230 if (!msg->trx) {
231 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n",
232 hexdump(msg->data, msg->len));
Harald Welteeab33352009-06-27 03:09:08 +0200233 talloc_free(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000234 return -EINVAL;
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100235 } else if (!msg->trx->rsl_link) {
236 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n",
237 hexdump(msg->data, msg->len));
238 talloc_free(msg);
239 return -EIO;
Harald Welte1fa60c82009-02-09 18:13:26 +0000240 }
241
242 sign_link = msg->trx->rsl_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000243 e1i_ts = sign_link->ts;
244 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
245 /* notify the driver we have something to write */
246 e1inp_driver = sign_link->ts->line->driver;
247 e1inp_driver->want_write(e1i_ts);
248 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000249 msgb_enqueue(&sign_link->tx_list, msg);
250
Holger Freyther0469cf62009-03-31 12:14:16 +0000251 /* dump it */
252 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
253
Harald Welte1fa60c82009-02-09 18:13:26 +0000254 return 0;
255}
256
Harald Welted88a3872011-02-14 15:26:13 +0100257int _abis_nm_sendmsg(struct msgb *msg, int to_trx_oml)
Harald Welte1fa60c82009-02-09 18:13:26 +0000258{
259 struct e1inp_sign_link *sign_link;
260 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000261 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000262
263 msg->l2h = msg->data;
264
265 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Harald Weltef933de92011-02-05 20:18:18 +0100266 LOGP(DNM, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000267 return -EINVAL;
268 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000269
Harald Welte15ccc772011-02-13 19:36:18 +0100270 /* Check for TRX-specific OML link first */
Harald Welted88a3872011-02-14 15:26:13 +0100271 if (to_trx_oml) {
272 if (!msg->trx->oml_link)
273 return -ENODEV;
Harald Welte15ccc772011-02-13 19:36:18 +0100274 sign_link = msg->trx->oml_link;
Harald Welted88a3872011-02-14 15:26:13 +0100275 } else
Harald Welte15ccc772011-02-13 19:36:18 +0100276 sign_link = msg->trx->bts->oml_link;
277
Harald Weltef55b49f2009-05-23 06:20:41 +0000278 e1i_ts = sign_link->ts;
279 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
280 /* notify the driver we have something to write */
281 e1inp_driver = sign_link->ts->line->driver;
282 e1inp_driver->want_write(e1i_ts);
283 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000284 msgb_enqueue(&sign_link->tx_list, msg);
285
Holger Freyther0469cf62009-03-31 12:14:16 +0000286 /* dump it */
287 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
288
Harald Welte1fa60c82009-02-09 18:13:26 +0000289 return 0;
290}
291
292/* Timeslot */
293
294/* configure and initialize one e1inp_ts */
295int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
296 enum e1inp_ts_type type)
297{
Harald Welte42581822009-08-08 16:12:58 +0200298 if (ts->type == type && ts->line && line)
299 return 0;
300
Harald Welte1fa60c82009-02-09 18:13:26 +0000301 ts->type = type;
302 ts->line = line;
303
304 switch (type) {
305 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther91f587e2011-01-16 18:12:13 +0100306 if (line && line->driver)
Holger Hans Peter Freytherd85642a2010-04-26 16:02:04 +0800307 ts->sign.delay = line->driver->default_delay;
308 else
309 ts->sign.delay = 100000;
Harald Welte1fa60c82009-02-09 18:13:26 +0000310 INIT_LLIST_HEAD(&ts->sign.sign_links);
311 break;
312 case E1INP_TS_TYPE_TRAU:
313 subchan_mux_init(&ts->trau.mux);
314 ts->trau.demux.out_cb = subch_cb;
315 ts->trau.demux.data = ts;
316 subch_demux_init(&ts->trau.demux);
317 break;
318 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100319 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte1fa60c82009-02-09 18:13:26 +0000320 ts->type);
321 return -EINVAL;
322 }
323 return 0;
324}
325
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200326struct e1inp_line *e1inp_line_get(uint8_t e1_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000327{
328 struct e1inp_line *e1i_line;
329
330 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000331 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000332 if (e1i_line->num == e1_nr)
333 return e1i_line;
334 }
335 return NULL;
336}
337
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200338struct e1inp_line *e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Harald Welte3016d9f2011-02-05 13:54:41 +0100339{
340 struct e1inp_driver *driver;
341 struct e1inp_line *line;
342 int i;
343
344 line = e1inp_line_get(e1_nr);
Harald Welte07bb0da2011-02-05 15:57:42 +0100345 if (line) {
346 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
347 e1_nr);
Harald Welte3016d9f2011-02-05 13:54:41 +0100348 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100349 }
Harald Welte3016d9f2011-02-05 13:54:41 +0100350
351 driver = e1inp_driver_find(driver_name);
Harald Welte07bb0da2011-02-05 15:57:42 +0100352 if (!driver) {
353 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
354 driver_name);
Harald Welte3016d9f2011-02-05 13:54:41 +0100355 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100356 }
Harald Welte3016d9f2011-02-05 13:54:41 +0100357
358 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
359 if (!line)
360 return NULL;
361
362 line->driver = driver;
363
364 line->num = e1_nr;
365 for (i = 0; i < NUM_E1_TS; i++) {
366 line->ts[i].num = i+1;
367 line->ts[i].line = line;
368 }
369 llist_add_tail(&line->list, &e1inp_line_list);
370
371 return line;
372}
373
374#if 0
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200375struct e1inp_line *e1inp_line_get_create(uint8_t e1_nr)
Harald Welte42581822009-08-08 16:12:58 +0200376{
377 struct e1inp_line *line;
378 int i;
379
380 line = e1inp_line_get(e1_nr);
381 if (line)
382 return line;
383
384 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
385 if (!line)
386 return NULL;
387
388 line->num = e1_nr;
389 for (i = 0; i < NUM_E1_TS; i++) {
390 line->ts[i].num = i+1;
391 line->ts[i].line = line;
392 }
393 llist_add_tail(&line->list, &e1inp_line_list);
394
395 return line;
396}
Harald Welte3016d9f2011-02-05 13:54:41 +0100397#endif
Harald Welte42581822009-08-08 16:12:58 +0200398
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200399static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000400{
401 struct e1inp_line *e1i_line;
402
403 e1i_line = e1inp_line_get(e1_nr);
404 if (!e1i_line)
405 return NULL;
406
407 return &e1i_line->ts[ts_nr-1];
408}
409
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200410struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000411{
412 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
413
414 if (!e1i_ts)
415 return NULL;
416
417 return &e1i_ts->trau.mux;
418}
419
420/* Signalling Link */
421
422struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200423 uint8_t tei, uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000424{
425 struct e1inp_sign_link *link;
426
427 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
428 if (link->sapi == sapi && link->tei == tei)
429 return link;
430 }
431
432 return NULL;
433}
434
435/* create a new signalling link in a E1 timeslot */
436
437struct e1inp_sign_link *
438e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200439 struct gsm_bts_trx *trx, uint8_t tei,
440 uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000441{
442 struct e1inp_sign_link *link;
443
444 if (ts->type != E1INP_TS_TYPE_SIGN)
445 return NULL;
446
Harald Welte470ec292009-06-26 20:25:23 +0200447 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte1fa60c82009-02-09 18:13:26 +0000448 if (!link)
449 return NULL;
450
Harald Welte1fa60c82009-02-09 18:13:26 +0000451 link->ts = ts;
452 link->type = type;
453 INIT_LLIST_HEAD(&link->tx_list);
454 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000455 link->tei = tei;
456 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000457
458 llist_add_tail(&link->list, &ts->sign.sign_links);
459
460 return link;
461}
462
Harald Welte42581822009-08-08 16:12:58 +0200463void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
464{
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200465 struct msgb *msg;
466
Harald Welte42581822009-08-08 16:12:58 +0200467 llist_del(&link->list);
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200468 while (!llist_empty(&link->tx_list)) {
469 msg = msgb_dequeue(&link->tx_list);
470 msgb_free(msg);
471 }
472
Holger Hans Peter Freytherf51b9002010-04-11 17:18:02 +0200473 if (link->ts->type == E1INP_TS_TYPE_SIGN)
474 bsc_del_timer(&link->ts->sign.tx_timer);
475
Harald Welte42581822009-08-08 16:12:58 +0200476 talloc_free(link);
477}
478
Harald Welte1fa60c82009-02-09 18:13:26 +0000479/* the E1 driver tells us he has received something on a TS */
480int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200481 uint8_t tei, uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000482{
483 struct e1inp_sign_link *link;
Harald Welte09cefee2011-02-12 12:29:21 +0100484 struct gsm_bts *bts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000485 int ret;
486
487 switch (ts->type) {
488 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000489 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000490 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000491 link = e1inp_lookup_sign_link(ts, tei, sapi);
492 if (!link) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100493 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte1fa60c82009-02-09 18:13:26 +0000494 "tei %d, sapi %d\n", tei, sapi);
495 return -EINVAL;
496 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100497
Harald Weltedc5062b2010-03-26 21:28:59 +0800498 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte1fa60c82009-02-09 18:13:26 +0000499 switch (link->type) {
500 case E1INP_SIGN_OML:
501 msg->trx = link->trx;
Harald Welte09cefee2011-02-12 12:29:21 +0100502 bts = msg->trx->bts;
503 ret = bts->model->oml_rcvmsg(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000504 break;
505 case E1INP_SIGN_RSL:
506 msg->trx = link->trx;
507 ret = abis_rsl_rcvmsg(msg);
508 break;
509 default:
510 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100511 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000512 break;
513 }
514 break;
515 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000516 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000517 break;
518 default:
519 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100520 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000521 break;
522 }
523
524 return ret;
525}
526
527#define TSX_ALLOC_SIZE 4096
528
529/* called by driver if it wants to transmit on a given TS */
530struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
531 struct e1inp_sign_link **sign_link)
532{
533 struct e1inp_sign_link *link;
534 struct msgb *msg = NULL;
535 int len;
536
537 switch (e1i_ts->type) {
538 case E1INP_TS_TYPE_SIGN:
539 /* FIXME: implement this round robin */
540 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
541 msg = msgb_dequeue(&link->tx_list);
542 if (msg) {
543 if (sign_link)
544 *sign_link = link;
545 break;
546 }
547 }
548 break;
549 case E1INP_TS_TYPE_TRAU:
Harald Welte966636f2009-06-26 19:39:35 +0200550 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte1fa60c82009-02-09 18:13:26 +0000551 if (!msg)
552 return NULL;
553 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
554 msgb_put(msg, 40);
555 break;
556 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100557 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000558 return NULL;
559 }
560 return msg;
561}
562
563/* called by driver in case some kind of link state event */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200564int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000565{
566 struct e1inp_sign_link *link;
Harald Weltef338a032011-01-14 15:55:42 +0100567 struct input_signal_data isd;
Harald Welte1fa60c82009-02-09 18:13:26 +0000568
569 link = e1inp_lookup_sign_link(ts, tei, sapi);
570 if (!link)
571 return -EINVAL;
572
Harald Weltef338a032011-01-14 15:55:42 +0100573 isd.link_type = link->type;
574 isd.trx = link->trx;
Harald Weltebd3137c2011-02-13 19:44:21 +0100575 isd.tei = tei;
576 isd.sapi = sapi;
Harald Weltef338a032011-01-14 15:55:42 +0100577
578 /* report further upwards */
579 dispatch_signal(SS_INPUT, evt, &isd);
Holger Freytherff9592f2009-03-09 16:17:14 +0000580 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000581}
582
583/* register a driver with the E1 core */
584int e1inp_driver_register(struct e1inp_driver *drv)
585{
Harald Welte44d542e2009-03-10 18:24:35 +0000586 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000587 return 0;
588}
589
Harald Welte3016d9f2011-02-05 13:54:41 +0100590struct e1inp_driver *e1inp_driver_find(const char *name)
591{
592 struct e1inp_driver *drv;
593
Harald Welte3016d9f2011-02-05 13:54:41 +0100594 llist_for_each_entry(drv, &e1inp_driver_list, list) {
595 if (!strcasecmp(name, drv->name))
596 return drv;
597 }
598 return NULL;
599}
600
Harald Welte42581822009-08-08 16:12:58 +0200601int e1inp_line_update(struct e1inp_line *line)
Harald Welte1fa60c82009-02-09 18:13:26 +0000602{
Harald Weltef27d0432011-02-11 16:49:41 +0100603 struct input_signal_data isd;
604 int rc;
605
Harald Welte54552432011-02-05 12:26:55 +0100606 if (line->driver && line->driver->line_update)
Harald Weltef27d0432011-02-11 16:49:41 +0100607 rc = line->driver->line_update(line);
Harald Welte54552432011-02-05 12:26:55 +0100608 else
Harald Weltef27d0432011-02-11 16:49:41 +0100609 rc = 0;
610
611 /* Send a signal to anyone who is interested in new lines being
612 * configured */
613 memset(&isd, 0, sizeof(isd));
614 isd.line = line;
615 dispatch_signal(SS_INPUT, S_INP_LINE_INIT, &isd);
616
617 return rc;
Harald Welte1fa60c82009-02-09 18:13:26 +0000618}
Harald Welte7bfc2672009-07-28 00:41:45 +0200619
Harald Welte29b9cf82009-11-30 19:43:54 +0100620static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
621 void *handler_data, void *signal_data)
622{
623 if (subsys != SS_GLOBAL ||
624 signal != S_GLOBAL_SHUTDOWN)
625 return 0;
626
627 if (pcap_fd) {
628 close(pcap_fd);
629 pcap_fd = -1;
630 }
631
632 return 0;
633}
634
Harald Welte3016d9f2011-02-05 13:54:41 +0100635void e1inp_misdn_init(void);
Harald Welte1dd68c32011-02-05 13:58:46 +0100636void e1inp_dahdi_init(void);
Harald Welte3016d9f2011-02-05 13:54:41 +0100637
638void e1inp_init(void)
Harald Welte7bfc2672009-07-28 00:41:45 +0200639{
640 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
641 "e1inp_sign_link");
Harald Welte29b9cf82009-11-30 19:43:54 +0100642 register_signal_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Welte3016d9f2011-02-05 13:54:41 +0100643
644 e1inp_misdn_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100645#ifdef HAVE_DAHDI_USER_H
Harald Welte1dd68c32011-02-05 13:58:46 +0100646 e1inp_dahdi_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100647#endif
Harald Welte7bfc2672009-07-28 00:41:45 +0200648}