blob: eab418a248008f7e95881e00d4b80cf044dda340 [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>
Harald Welte0000ca52011-08-11 12:50:13 +020042#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010043#include <osmocom/core/msgb.h>
Harald Welte0000ca52011-08-11 12:50:13 +020044#include <osmocom/core/talloc.h>
45#include <osmocom/core/rate_ctr.h>
46
Harald Welte1fa60c82009-02-09 18:13:26 +000047#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 Welte1fa60c82009-02-09 18:13:26 +000052#include <openbsc/subchan_demux.h>
Harald Welte45b407a2009-05-23 15:51:12 +000053#include <openbsc/trau_frame.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000054#include <openbsc/trau_mux.h>
Harald Welte29b9cf82009-11-30 19:43:54 +010055#include <openbsc/signal.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020056#include <openbsc/misdn.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000057
Harald Weltec08e8be2011-03-04 13:53:51 +010058#include "../../bscconfig.h"
Harald Welteca17ef82011-02-05 15:13:27 +010059
Harald Welte1fa60c82009-02-09 18:13:26 +000060#define NUM_E1_TS 32
61
62/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000063LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000064
65/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000066LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000067
Harald Welte2cf161b2009-06-20 22:36:41 +020068static void *tall_sigl_ctx;
69
Harald Welte0000ca52011-08-11 12:50:13 +020070static const struct rate_ctr_desc e1inp_ctr_d[] = {
71 [E1I_CTR_HDLC_ABORT] = {
Harald Weltec21aa182011-08-11 12:54:07 +020072 "hdlc.abort", "HDLC abort"
Harald Welte0000ca52011-08-11 12:50:13 +020073 },
74 [E1I_CTR_HDLC_BADFCS] = {
Harald Weltec21aa182011-08-11 12:54:07 +020075 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Welte0000ca52011-08-11 12:50:13 +020076 },
77 [E1I_CTR_HDLC_OVERR] = {
78 "hdlc.overrun", "HDLC Overrun"
79 },
80 [E1I_CTR_ALARM] = {
Harald Weltec21aa182011-08-11 12:54:07 +020081 "alarm", "Alarm"
Harald Welte0000ca52011-08-11 12:50:13 +020082 },
83 [E1I_CTR_REMOVED] = {
Harald Weltec21aa182011-08-11 12:54:07 +020084 "removed", "Line removed"
Harald Welte0000ca52011-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 Welte1fa60c82009-02-09 18:13:26 +000095/*
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 Freytherc42ad8b2011-04-18 17:04:00 +0200104 uint32_t magic_number;
105 uint16_t version_major;
106 uint16_t version_minor;
Harald Welte1fa60c82009-02-09 18:13:26 +0000107 int32_t thiszone;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200108 uint32_t sigfigs;
109 uint32_t snaplen;
110 uint32_t network;
Harald Welte1fa60c82009-02-09 18:13:26 +0000111} __attribute__((packed));
112
113struct pcaprec_hdr {
Holger Hans Peter Freytherc42ad8b2011-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 Welte1fa60c82009-02-09 18:13:26 +0000118} __attribute__((packed));
119
120struct fake_linux_lapd_header {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200121 uint16_t pkttype;
122 uint16_t hatype;
123 uint16_t halen;
124 uint64_t addr;
Harald Welte1fa60c82009-02-09 18:13:26 +0000125 int16_t protocol;
126} __attribute__((packed));
127
128struct lapd_header {
Holger Hans Peter Freytherc42ad8b2011-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 Welte1fa60c82009-02-09 18:13:26 +0000135} __attribute__((packed));
136
Pablo Neira Ayusoc0d17f22011-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 Welte1fa60c82009-02-09 18:13:26 +0000142
143
144static int pcap_fd = -1;
145
Holger Freyther0469cf62009-03-31 12:14:16 +0000146void e1_set_pcap_fd(int fd)
Harald Welte1fa60c82009-02-09 18:13:26 +0000147{
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 */
Holger Freyther0469cf62009-03-31 12:14:16 +0000164static void write_pcap_packet(int direction, int sapi, int tei,
Harald Welte1fa60c82009-02-09 18:13:26 +0000165 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,
Holger Freyther0469cf62009-03-31 12:14:16 +0000184 .sapi = sapi & 0x3F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000185 .ea2 = 1,
Holger Freyther0469cf62009-03-31 12:14:16 +0000186 .tei = tei & 0x7F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000187 .control_foo = 0x03 /* UI */,
188 };
189
190 struct pcaprec_hdr payload_header = {
191 .ts_sec = 0,
192 .ts_usec = 0,
Harald Welte95aa5c42011-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 Welte1fa60c82009-02-09 18:13:26 +0000197 };
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 Welte95aa5c42011-02-12 14:00:23 +0100207 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000208}
Harald Welte1fa60c82009-02-09 18:13:26 +0000209
Harald Welted256d4f2009-03-10 19:44:48 +0000210static 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
Harald Welte1fa60c82009-02-09 18:13:26 +0000235/* callback when a TRAU frame was received */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200236static int subch_cb(struct subch_demux *dmx, int ch, uint8_t *data, int len,
Harald Welte1fa60c82009-02-09 18:13:26 +0000237 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 Ayuso7abecfc2011-08-17 22:43:54 +0200249int abis_sendmsg(struct msgb *msg)
Harald Welte1fa60c82009-02-09 18:13:26 +0000250{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200251 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte1fa60c82009-02-09 18:13:26 +0000252 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000253 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000254
255 msg->l2h = msg->data;
256
Pablo Neira Ayuso7abecfc2011-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 Ayusoc0d17f22011-05-07 12:12:48 +0200260 osmo_hexdump(msg->data, msg->len));
Harald Welteeab33352009-06-27 03:09:08 +0200261 talloc_free(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000262 return -EINVAL;
Pablo Neira Ayuso7abecfc2011-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 Ayusoc0d17f22011-05-07 12:12:48 +0200288 osmo_hexdump(msg->data, msg->len));
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100289 talloc_free(msg);
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200290 return -EINVAL;
Harald Welte1fa60c82009-02-09 18:13:26 +0000291 }
Harald Weltef55b49f2009-05-23 06:20:41 +0000292 e1i_ts = sign_link->ts;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200293 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
Harald Weltef55b49f2009-05-23 06:20:41 +0000294 /* notify the driver we have something to write */
295 e1inp_driver = sign_link->ts->line->driver;
296 e1inp_driver->want_write(e1i_ts);
297 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000298 msgb_enqueue(&sign_link->tx_list, msg);
299
Holger Freyther0469cf62009-03-31 12:14:16 +0000300 /* dump it */
301 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
302
Harald Welte1fa60c82009-02-09 18:13:26 +0000303 return 0;
304}
305
Harald Welted88a3872011-02-14 15:26:13 +0100306int _abis_nm_sendmsg(struct msgb *msg, int to_trx_oml)
Harald Welte1fa60c82009-02-09 18:13:26 +0000307{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200308 struct e1inp_sign_link *sign_link = msg->dst;
Harald Welte1fa60c82009-02-09 18:13:26 +0000309
310 msg->l2h = msg->data;
311
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200312 if (!msg->dst) {
313 LOGP(DNM, LOGL_ERROR, "_abis_nm_sendmsg: msg->dst == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000314 return -EINVAL;
315 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000316
Harald Welte15ccc772011-02-13 19:36:18 +0100317 /* Check for TRX-specific OML link first */
Harald Welted88a3872011-02-14 15:26:13 +0100318 if (to_trx_oml) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200319 if (!sign_link->trx->oml_link)
Harald Welted88a3872011-02-14 15:26:13 +0100320 return -ENODEV;
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200321 msg->dst = sign_link->trx->oml_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000322 }
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200323 return abis_sendmsg(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000324}
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 Welte42581822009-08-08 16:12:58 +0200332 if (ts->type == type && ts->line && line)
333 return 0;
334
Harald Welte1fa60c82009-02-09 18:13:26 +0000335 ts->type = type;
336 ts->line = line;
337
338 switch (type) {
339 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther91f587e2011-01-16 18:12:13 +0100340 if (line && line->driver)
Holger Hans Peter Freytherd85642a2010-04-26 16:02:04 +0800341 ts->sign.delay = line->driver->default_delay;
342 else
343 ts->sign.delay = 100000;
Harald Welte1fa60c82009-02-09 18:13:26 +0000344 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 Welteb1d4c8e2009-12-17 23:10:46 +0100353 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte1fa60c82009-02-09 18:13:26 +0000354 ts->type);
355 return -EINVAL;
356 }
357 return 0;
358}
359
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200360struct e1inp_line *e1inp_line_get(uint8_t e1_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000361{
362 struct e1inp_line *e1i_line;
363
364 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000365 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000366 if (e1i_line->num == e1_nr)
367 return e1i_line;
368 }
369 return NULL;
370}
371
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200372struct e1inp_line *e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Harald Welte3016d9f2011-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 Welte07bb0da2011-02-05 15:57:42 +0100379 if (line) {
380 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
381 e1_nr);
Harald Welte3016d9f2011-02-05 13:54:41 +0100382 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100383 }
Harald Welte3016d9f2011-02-05 13:54:41 +0100384
385 driver = e1inp_driver_find(driver_name);
Harald Welte07bb0da2011-02-05 15:57:42 +0100386 if (!driver) {
387 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
388 driver_name);
Harald Welte3016d9f2011-02-05 13:54:41 +0100389 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100390 }
Harald Welte3016d9f2011-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 Welte3016d9f2011-02-05 13:54:41 +0100397 line->num = e1_nr;
Harald Welte0000ca52011-08-11 12:50:13 +0200398
399 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
400
Harald Welte3016d9f2011-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 Freytherc42ad8b2011-04-18 17:04:00 +0200411struct e1inp_line *e1inp_line_get_create(uint8_t e1_nr)
Harald Welte42581822009-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 Welte3016d9f2011-02-05 13:54:41 +0100433#endif
Harald Welte42581822009-08-08 16:12:58 +0200434
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200435static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000436{
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 Freytherc42ad8b2011-04-18 17:04:00 +0200446struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000447{
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 Freytherc42ad8b2011-04-18 17:04:00 +0200459 uint8_t tei, uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000460{
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 Freytherc42ad8b2011-04-18 17:04:00 +0200475 struct gsm_bts_trx *trx, uint8_t tei,
476 uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000477{
478 struct e1inp_sign_link *link;
479
480 if (ts->type != E1INP_TS_TYPE_SIGN)
481 return NULL;
482
Harald Welte470ec292009-06-26 20:25:23 +0200483 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte1fa60c82009-02-09 18:13:26 +0000484 if (!link)
485 return NULL;
486
Harald Welte1fa60c82009-02-09 18:13:26 +0000487 link->ts = ts;
488 link->type = type;
489 INIT_LLIST_HEAD(&link->tx_list);
490 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000491 link->tei = tei;
492 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000493
494 llist_add_tail(&link->list, &ts->sign.sign_links);
495
496 return link;
497}
498
Harald Welte42581822009-08-08 16:12:58 +0200499void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
500{
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200501 struct msgb *msg;
502
Harald Welte42581822009-08-08 16:12:58 +0200503 llist_del(&link->list);
Holger Hans Peter Freyther4ac100e2010-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 Freytherf51b9002010-04-11 17:18:02 +0200509 if (link->ts->type == E1INP_TS_TYPE_SIGN)
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200510 osmo_timer_del(&link->ts->sign.tx_timer);
Holger Hans Peter Freytherf51b9002010-04-11 17:18:02 +0200511
Harald Welte42581822009-08-08 16:12:58 +0200512 talloc_free(link);
513}
514
Harald Welte1fa60c82009-02-09 18:13:26 +0000515/* 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 Freytherc42ad8b2011-04-18 17:04:00 +0200517 uint8_t tei, uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000518{
519 struct e1inp_sign_link *link;
Harald Welte09cefee2011-02-12 12:29:21 +0100520 struct gsm_bts *bts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000521 int ret;
522
523 switch (ts->type) {
524 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000525 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000526 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000527 link = e1inp_lookup_sign_link(ts, tei, sapi);
528 if (!link) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100529 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte1fa60c82009-02-09 18:13:26 +0000530 "tei %d, sapi %d\n", tei, sapi);
531 return -EINVAL;
532 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100533
Harald Weltedc5062b2010-03-26 21:28:59 +0800534 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte1fa60c82009-02-09 18:13:26 +0000535 switch (link->type) {
536 case E1INP_SIGN_OML:
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200537 msg->dst = link;
538 bts = link->trx->bts;
Harald Welte09cefee2011-02-12 12:29:21 +0100539 ret = bts->model->oml_rcvmsg(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000540 break;
541 case E1INP_SIGN_RSL:
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200542 msg->dst = link;
Harald Welte1fa60c82009-02-09 18:13:26 +0000543 ret = abis_rsl_rcvmsg(msg);
544 break;
545 default:
546 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100547 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000548 break;
549 }
550 break;
551 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000552 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000553 break;
554 default:
555 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100556 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000557 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 Welte966636f2009-06-26 19:39:35 +0200586 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte1fa60c82009-02-09 18:13:26 +0000587 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 Welteb1d4c8e2009-12-17 23:10:46 +0100593 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000594 return NULL;
595 }
596 return msg;
597}
598
599/* called by driver in case some kind of link state event */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200600int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
Harald Welte1fa60c82009-02-09 18:13:26 +0000601{
602 struct e1inp_sign_link *link;
Harald Weltef338a032011-01-14 15:55:42 +0100603 struct input_signal_data isd;
Harald Welte1fa60c82009-02-09 18:13:26 +0000604
605 link = e1inp_lookup_sign_link(ts, tei, sapi);
606 if (!link)
607 return -EINVAL;
608
Pablo Neira Ayusocd986562011-08-09 23:15:38 +0200609 isd.line = ts->line;
Harald Weltef338a032011-01-14 15:55:42 +0100610 isd.link_type = link->type;
611 isd.trx = link->trx;
Harald Weltebd3137c2011-02-13 19:44:21 +0100612 isd.tei = tei;
613 isd.sapi = sapi;
Harald Weltef338a032011-01-14 15:55:42 +0100614
615 /* report further upwards */
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200616 osmo_signal_dispatch(SS_INPUT, evt, &isd);
Holger Freytherff9592f2009-03-09 16:17:14 +0000617 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000618}
619
620/* register a driver with the E1 core */
621int e1inp_driver_register(struct e1inp_driver *drv)
622{
Harald Welte44d542e2009-03-10 18:24:35 +0000623 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000624 return 0;
625}
626
Harald Welte3016d9f2011-02-05 13:54:41 +0100627struct e1inp_driver *e1inp_driver_find(const char *name)
628{
629 struct e1inp_driver *drv;
630
Harald Welte3016d9f2011-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 Welte42581822009-08-08 16:12:58 +0200638int e1inp_line_update(struct e1inp_line *line)
Harald Welte1fa60c82009-02-09 18:13:26 +0000639{
Harald Weltef27d0432011-02-11 16:49:41 +0100640 struct input_signal_data isd;
641 int rc;
642
Harald Welte54552432011-02-05 12:26:55 +0100643 if (line->driver && line->driver->line_update)
Harald Weltef27d0432011-02-11 16:49:41 +0100644 rc = line->driver->line_update(line);
Harald Welte54552432011-02-05 12:26:55 +0100645 else
Harald Weltef27d0432011-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 Ayusobbc5b992011-05-06 12:12:31 +0200652 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_INIT, &isd);
Harald Weltef27d0432011-02-11 16:49:41 +0100653
654 return rc;
Harald Welte1fa60c82009-02-09 18:13:26 +0000655}
Harald Welte7bfc2672009-07-28 00:41:45 +0200656
Harald Welte29b9cf82009-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 Welte3016d9f2011-02-05 13:54:41 +0100672void e1inp_misdn_init(void);
Harald Welte1dd68c32011-02-05 13:58:46 +0100673void e1inp_dahdi_init(void);
Pablo Neira Ayusoc68f7542011-05-14 11:32:47 +0200674void e1inp_ipaccess_init(void);
675void e1inp_hsl_init(void);
Harald Welte3016d9f2011-02-05 13:54:41 +0100676
677void e1inp_init(void)
Harald Welte7bfc2672009-07-28 00:41:45 +0200678{
679 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
680 "e1inp_sign_link");
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200681 osmo_signal_register_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Welte3016d9f2011-02-05 13:54:41 +0100682
683 e1inp_misdn_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100684#ifdef HAVE_DAHDI_USER_H
Harald Welte1dd68c32011-02-05 13:58:46 +0100685 e1inp_dahdi_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100686#endif
Pablo Neira Ayusoc68f7542011-05-14 11:32:47 +0200687 e1inp_ipaccess_init();
688 e1inp_hsl_init();
Harald Welte7bfc2672009-07-28 00:41:45 +0200689}