blob: 7fbf1797a4dd889c7651db66bbf4658c0d56d3ac [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
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdio.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <string.h>
28#include <time.h>
29#include <sys/fcntl.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/ioctl.h>
33#include <arpa/inet.h>
34#include <mISDNif.h>
35
36//#define AF_COMPATIBILITY_FUNC
37//#include <compat_af_isdn.h>
Holger Freyther66e092b2009-03-31 12:13:50 +000038#ifndef AF_ISDN
Harald Welte1fa60c82009-02-09 18:13:26 +000039#define AF_ISDN 34
40#define PF_ISDN AF_ISDN
Holger Freyther66e092b2009-03-31 12:13:50 +000041#endif
Harald Welte1fa60c82009-02-09 18:13:26 +000042
43#include <openbsc/select.h>
44#include <openbsc/msgb.h>
45#include <openbsc/debug.h>
46#include <openbsc/gsm_data.h>
47#include <openbsc/e1_input.h>
48#include <openbsc/abis_nm.h>
49#include <openbsc/abis_rsl.h>
50#include <openbsc/linuxlist.h>
51#include <openbsc/subchan_demux.h>
52#include <openbsc/trau_mux.h>
53
54#define NUM_E1_TS 32
55
56/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000057LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000058
59/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000060LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000061
Holger Freytherff9592f2009-03-09 16:17:14 +000062/* to be implemented, e.g. by bsc_hack.c */
63void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx);
64
Harald Welte1fa60c82009-02-09 18:13:26 +000065/*
66 * pcap writing of the misdn load
67 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
68 */
69#define DLT_LINUX_LAPD 177
70#define PCAP_INPUT 0
71#define PCAP_OUTPUT 1
72
73struct pcap_hdr {
74 u_int32_t magic_number;
75 u_int16_t version_major;
76 u_int16_t version_minor;
77 int32_t thiszone;
78 u_int32_t sigfigs;
79 u_int32_t snaplen;
80 u_int32_t network;
81} __attribute__((packed));
82
83struct pcaprec_hdr {
84 u_int32_t ts_sec;
85 u_int32_t ts_usec;
86 u_int32_t incl_len;
87 u_int32_t orig_len;
88} __attribute__((packed));
89
90struct fake_linux_lapd_header {
91 u_int16_t pkttype;
92 u_int16_t hatype;
93 u_int16_t halen;
94 u_int64_t addr;
95 int16_t protocol;
96} __attribute__((packed));
97
98struct lapd_header {
99 u_int8_t ea1 : 1;
100 u_int8_t cr : 1;
101 u_int8_t sapi : 6;
102 u_int8_t ea2 : 1;
103 u_int8_t tei : 7;
104 u_int8_t control_foo; /* fake UM's ... */
105} __attribute__((packed));
106
107static_assert((int)&((struct fake_linux_lapd_header*)NULL)->hatype == 2, hatype_offset);
108static_assert((int)&((struct fake_linux_lapd_header*)NULL)->halen == 4, halen_offset);
109static_assert((int)&((struct fake_linux_lapd_header*)NULL)->addr == 6, addr_offset);
110static_assert((int)&((struct fake_linux_lapd_header*)NULL)->protocol == 14, proto_offset);
111static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
112
113
114static int pcap_fd = -1;
115
Holger Freyther0469cf62009-03-31 12:14:16 +0000116void e1_set_pcap_fd(int fd)
Harald Welte1fa60c82009-02-09 18:13:26 +0000117{
118 int ret;
119 struct pcap_hdr header = {
120 .magic_number = 0xa1b2c3d4,
121 .version_major = 2,
122 .version_minor = 4,
123 .thiszone = 0,
124 .sigfigs = 0,
125 .snaplen = 65535,
126 .network = DLT_LINUX_LAPD,
127 };
128
129 pcap_fd = fd;
130 ret = write(pcap_fd, &header, sizeof(header));
131}
132
133/* This currently only works for the D-Channel */
Holger Freyther0469cf62009-03-31 12:14:16 +0000134static void write_pcap_packet(int direction, int sapi, int tei,
Harald Welte1fa60c82009-02-09 18:13:26 +0000135 struct msgb *msg) {
136 if (pcap_fd < 0)
137 return;
138
139 int ret;
140 time_t cur_time;
141 struct tm *tm;
142
143 struct fake_linux_lapd_header header = {
144 .pkttype = 4,
145 .hatype = 0,
146 .halen = 0,
147 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
148 .protocol = ntohs(48),
149 };
150
151 struct lapd_header lapd_header = {
152 .ea1 = 0,
153 .cr = direction == PCAP_OUTPUT ? 1 : 0,
Holger Freyther0469cf62009-03-31 12:14:16 +0000154 .sapi = sapi & 0x3F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000155 .ea2 = 1,
Holger Freyther0469cf62009-03-31 12:14:16 +0000156 .tei = tei & 0x7F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000157 .control_foo = 0x03 /* UI */,
158 };
159
160 struct pcaprec_hdr payload_header = {
161 .ts_sec = 0,
162 .ts_usec = 0,
163 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
164 + sizeof(struct lapd_header)
165 - MISDN_HEADER_LEN,
166 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
167 + sizeof(struct lapd_header)
168 - MISDN_HEADER_LEN,
169 };
170
171
172 cur_time = time(NULL);
173 tm = localtime(&cur_time);
174 payload_header.ts_sec = mktime(tm);
175
176 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
177 ret = write(pcap_fd, &header, sizeof(header));
178 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
179 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
180 msg->len - MISDN_HEADER_LEN);
181}
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 */
209static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
210 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
230 if (!msg->trx || !msg->trx->rsl_link) {
231 fprintf(stderr, "rsl_sendmsg: msg->trx == NULL\n");
232 return -EINVAL;
233 }
234
235 sign_link = msg->trx->rsl_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000236 e1i_ts = sign_link->ts;
237 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
238 /* notify the driver we have something to write */
239 e1inp_driver = sign_link->ts->line->driver;
240 e1inp_driver->want_write(e1i_ts);
241 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000242 msgb_enqueue(&sign_link->tx_list, msg);
243
Holger Freyther0469cf62009-03-31 12:14:16 +0000244 /* dump it */
245 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
246
Harald Welte1fa60c82009-02-09 18:13:26 +0000247 return 0;
248}
249
250int _abis_nm_sendmsg(struct msgb *msg)
251{
252 struct e1inp_sign_link *sign_link;
253 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000254 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000255
256 msg->l2h = msg->data;
257
258 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Holger Freytherfb3f5192009-02-09 23:09:15 +0000259 fprintf(stderr, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000260 return -EINVAL;
261 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000262
Harald Welte1fa60c82009-02-09 18:13:26 +0000263 sign_link = msg->trx->bts->oml_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000264 e1i_ts = sign_link->ts;
265 if (!bsc_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 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000270 msgb_enqueue(&sign_link->tx_list, msg);
271
Holger Freyther0469cf62009-03-31 12:14:16 +0000272 /* dump it */
273 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
274
Harald Welte1fa60c82009-02-09 18:13:26 +0000275 return 0;
276}
277
278/* Timeslot */
279
280/* configure and initialize one e1inp_ts */
281int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
282 enum e1inp_ts_type type)
283{
284 ts->type = type;
285 ts->line = line;
286
287 switch (type) {
288 case E1INP_TS_TYPE_SIGN:
289 INIT_LLIST_HEAD(&ts->sign.sign_links);
290 break;
291 case E1INP_TS_TYPE_TRAU:
292 subchan_mux_init(&ts->trau.mux);
293 ts->trau.demux.out_cb = subch_cb;
294 ts->trau.demux.data = ts;
295 subch_demux_init(&ts->trau.demux);
296 break;
297 default:
298 fprintf(stderr, "unsupported E1 timeslot type %u\n",
299 ts->type);
300 return -EINVAL;
301 }
302 return 0;
303}
304
305static struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
306{
307 struct e1inp_line *e1i_line;
308
309 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000310 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000311 if (e1i_line->num == e1_nr)
312 return e1i_line;
313 }
314 return NULL;
315}
316
317static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
318{
319 struct e1inp_line *e1i_line;
320
321 e1i_line = e1inp_line_get(e1_nr);
322 if (!e1i_line)
323 return NULL;
324
325 return &e1i_line->ts[ts_nr-1];
326}
327
328struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
329{
330 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
331
332 if (!e1i_ts)
333 return NULL;
334
335 return &e1i_ts->trau.mux;
336}
337
338/* Signalling Link */
339
340struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
341 u_int8_t tei, u_int8_t sapi)
342{
343 struct e1inp_sign_link *link;
344
345 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
346 if (link->sapi == sapi && link->tei == tei)
347 return link;
348 }
349
350 return NULL;
351}
352
353/* create a new signalling link in a E1 timeslot */
354
355struct e1inp_sign_link *
356e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
357 struct gsm_bts_trx *trx, u_int8_t tei,
358 u_int8_t sapi)
359{
360 struct e1inp_sign_link *link;
361
362 if (ts->type != E1INP_TS_TYPE_SIGN)
363 return NULL;
364
365 link = malloc(sizeof(*link));
366 if (!link)
367 return NULL;
368
369 memset(link, 0, sizeof(*link));
370
371 link->ts = ts;
372 link->type = type;
373 INIT_LLIST_HEAD(&link->tx_list);
374 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000375 link->tei = tei;
376 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000377
378 llist_add_tail(&link->list, &ts->sign.sign_links);
379
380 return link;
381}
382
383/* the E1 driver tells us he has received something on a TS */
384int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
385 u_int8_t tei, u_int8_t sapi)
386{
387 struct e1inp_sign_link *link;
388 int ret;
389
390 switch (ts->type) {
391 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000392 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000393 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000394 link = e1inp_lookup_sign_link(ts, tei, sapi);
395 if (!link) {
396 fprintf(stderr, "didn't find singalling link for "
397 "tei %d, sapi %d\n", tei, sapi);
398 return -EINVAL;
399 }
400 switch (link->type) {
401 case E1INP_SIGN_OML:
402 msg->trx = link->trx;
403 ret = abis_nm_rcvmsg(msg);
404 break;
405 case E1INP_SIGN_RSL:
406 msg->trx = link->trx;
407 ret = abis_rsl_rcvmsg(msg);
408 break;
409 default:
410 ret = -EINVAL;
411 fprintf(stderr, "unknown link type %u\n", link->type);
412 break;
413 }
414 break;
415 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000416 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000417 break;
418 default:
419 ret = -EINVAL;
420 fprintf(stderr, "unknown TS type %u\n", ts->type);
421 break;
422 }
423
424 return ret;
425}
426
427#define TSX_ALLOC_SIZE 4096
428
429/* called by driver if it wants to transmit on a given TS */
430struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
431 struct e1inp_sign_link **sign_link)
432{
433 struct e1inp_sign_link *link;
434 struct msgb *msg = NULL;
435 int len;
436
437 switch (e1i_ts->type) {
438 case E1INP_TS_TYPE_SIGN:
439 /* FIXME: implement this round robin */
440 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
441 msg = msgb_dequeue(&link->tx_list);
442 if (msg) {
443 if (sign_link)
444 *sign_link = link;
445 break;
446 }
447 }
448 break;
449 case E1INP_TS_TYPE_TRAU:
450 msg = msgb_alloc(TSX_ALLOC_SIZE);
451 if (!msg)
452 return NULL;
453 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
454 msgb_put(msg, 40);
455 break;
456 default:
457 fprintf(stderr, "unsupported E1 TS type %u\n", e1i_ts->type);
458 return NULL;
459 }
460 return msg;
461}
462
463/* called by driver in case some kind of link state event */
464int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
465{
466 struct e1inp_sign_link *link;
467
468 link = e1inp_lookup_sign_link(ts, tei, sapi);
469 if (!link)
470 return -EINVAL;
471
472 /* FIXME: report further upwards */
Holger Freytherff9592f2009-03-09 16:17:14 +0000473 input_event(evt, link->type, link->trx);
474 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000475}
476
477/* register a driver with the E1 core */
478int e1inp_driver_register(struct e1inp_driver *drv)
479{
Harald Welte44d542e2009-03-10 18:24:35 +0000480 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000481 return 0;
482}
483
484/* register a line with the E1 core */
485int e1inp_line_register(struct e1inp_line *line)
486{
487 int i;
488
Harald Welted256d4f2009-03-10 19:44:48 +0000489 for (i = 0; i < NUM_E1_TS; i++) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000490 line->ts[i].num = i+1;
Harald Welted256d4f2009-03-10 19:44:48 +0000491 line->ts[i].line = line;
492 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000493
Harald Welte44d542e2009-03-10 18:24:35 +0000494 llist_add_tail(&line->list, &e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000495
496 return 0;
497}