blob: c3c7c75971e6c03a69187f21d5f107d155f73c82 [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>
Harald Welte45b407a2009-05-23 15:51:12 +000052#include <openbsc/trau_frame.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000053#include <openbsc/trau_mux.h>
54
55#define NUM_E1_TS 32
56
57/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000058LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000059
60/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000061LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000062
Holger Freytherff9592f2009-03-09 16:17:14 +000063/* to be implemented, e.g. by bsc_hack.c */
64void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx);
65
Harald Welte1fa60c82009-02-09 18:13:26 +000066/*
67 * pcap writing of the misdn load
68 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
69 */
70#define DLT_LINUX_LAPD 177
71#define PCAP_INPUT 0
72#define PCAP_OUTPUT 1
73
74struct pcap_hdr {
75 u_int32_t magic_number;
76 u_int16_t version_major;
77 u_int16_t version_minor;
78 int32_t thiszone;
79 u_int32_t sigfigs;
80 u_int32_t snaplen;
81 u_int32_t network;
82} __attribute__((packed));
83
84struct pcaprec_hdr {
85 u_int32_t ts_sec;
86 u_int32_t ts_usec;
87 u_int32_t incl_len;
88 u_int32_t orig_len;
89} __attribute__((packed));
90
91struct fake_linux_lapd_header {
92 u_int16_t pkttype;
93 u_int16_t hatype;
94 u_int16_t halen;
95 u_int64_t addr;
96 int16_t protocol;
97} __attribute__((packed));
98
99struct lapd_header {
100 u_int8_t ea1 : 1;
101 u_int8_t cr : 1;
102 u_int8_t sapi : 6;
103 u_int8_t ea2 : 1;
104 u_int8_t tei : 7;
105 u_int8_t control_foo; /* fake UM's ... */
106} __attribute__((packed));
107
108static_assert((int)&((struct fake_linux_lapd_header*)NULL)->hatype == 2, hatype_offset);
109static_assert((int)&((struct fake_linux_lapd_header*)NULL)->halen == 4, halen_offset);
110static_assert((int)&((struct fake_linux_lapd_header*)NULL)->addr == 6, addr_offset);
111static_assert((int)&((struct fake_linux_lapd_header*)NULL)->protocol == 14, proto_offset);
112static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
113
114
115static int pcap_fd = -1;
116
Holger Freyther0469cf62009-03-31 12:14:16 +0000117void e1_set_pcap_fd(int fd)
Harald Welte1fa60c82009-02-09 18:13:26 +0000118{
119 int ret;
120 struct pcap_hdr header = {
121 .magic_number = 0xa1b2c3d4,
122 .version_major = 2,
123 .version_minor = 4,
124 .thiszone = 0,
125 .sigfigs = 0,
126 .snaplen = 65535,
127 .network = DLT_LINUX_LAPD,
128 };
129
130 pcap_fd = fd;
131 ret = write(pcap_fd, &header, sizeof(header));
132}
133
134/* This currently only works for the D-Channel */
Holger Freyther0469cf62009-03-31 12:14:16 +0000135static void write_pcap_packet(int direction, int sapi, int tei,
Harald Welte1fa60c82009-02-09 18:13:26 +0000136 struct msgb *msg) {
137 if (pcap_fd < 0)
138 return;
139
140 int ret;
141 time_t cur_time;
142 struct tm *tm;
143
144 struct fake_linux_lapd_header header = {
145 .pkttype = 4,
146 .hatype = 0,
147 .halen = 0,
148 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
149 .protocol = ntohs(48),
150 };
151
152 struct lapd_header lapd_header = {
153 .ea1 = 0,
154 .cr = direction == PCAP_OUTPUT ? 1 : 0,
Holger Freyther0469cf62009-03-31 12:14:16 +0000155 .sapi = sapi & 0x3F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000156 .ea2 = 1,
Holger Freyther0469cf62009-03-31 12:14:16 +0000157 .tei = tei & 0x7F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000158 .control_foo = 0x03 /* UI */,
159 };
160
161 struct pcaprec_hdr payload_header = {
162 .ts_sec = 0,
163 .ts_usec = 0,
164 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
165 + sizeof(struct lapd_header)
166 - MISDN_HEADER_LEN,
167 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
168 + sizeof(struct lapd_header)
169 - MISDN_HEADER_LEN,
170 };
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));
180 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
181 msg->len - MISDN_HEADER_LEN);
182}
Harald Welte1fa60c82009-02-09 18:13:26 +0000183
Harald Welted256d4f2009-03-10 19:44:48 +0000184static const char *sign_types[] = {
185 [E1INP_SIGN_NONE] = "None",
186 [E1INP_SIGN_OML] = "OML",
187 [E1INP_SIGN_RSL] = "RSL",
188};
189const char *e1inp_signtype_name(enum e1inp_sign_type tp)
190{
191 if (tp >= ARRAY_SIZE(sign_types))
192 return "undefined";
193 return sign_types[tp];
194}
195
196static const char *ts_types[] = {
197 [E1INP_TS_TYPE_NONE] = "None",
198 [E1INP_TS_TYPE_SIGN] = "Signalling",
199 [E1INP_TS_TYPE_TRAU] = "TRAU",
200};
201
202const char *e1inp_tstype_name(enum e1inp_ts_type tp)
203{
204 if (tp >= ARRAY_SIZE(ts_types))
205 return "undefined";
206 return ts_types[tp];
207}
208
Harald Welte1fa60c82009-02-09 18:13:26 +0000209/* callback when a TRAU frame was received */
210static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
211 void *_priv)
212{
213 struct e1inp_ts *e1i_ts = _priv;
214 struct gsm_e1_subslot src_ss;
215
216 src_ss.e1_nr = e1i_ts->line->num;
217 src_ss.e1_ts = e1i_ts->num;
218 src_ss.e1_ts_ss = ch;
219
220 return trau_mux_input(&src_ss, data, len);
221}
222
223int abis_rsl_sendmsg(struct msgb *msg)
224{
225 struct e1inp_sign_link *sign_link;
226 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000227 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000228
229 msg->l2h = msg->data;
230
231 if (!msg->trx || !msg->trx->rsl_link) {
232 fprintf(stderr, "rsl_sendmsg: msg->trx == NULL\n");
233 return -EINVAL;
234 }
235
236 sign_link = msg->trx->rsl_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000237 e1i_ts = sign_link->ts;
238 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
239 /* notify the driver we have something to write */
240 e1inp_driver = sign_link->ts->line->driver;
241 e1inp_driver->want_write(e1i_ts);
242 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000243 msgb_enqueue(&sign_link->tx_list, msg);
244
Holger Freyther0469cf62009-03-31 12:14:16 +0000245 /* dump it */
246 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
247
Harald Welte1fa60c82009-02-09 18:13:26 +0000248 return 0;
249}
250
251int _abis_nm_sendmsg(struct msgb *msg)
252{
253 struct e1inp_sign_link *sign_link;
254 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000255 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000256
257 msg->l2h = msg->data;
258
259 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Holger Freytherfb3f5192009-02-09 23:09:15 +0000260 fprintf(stderr, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000261 return -EINVAL;
262 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000263
Harald Welte1fa60c82009-02-09 18:13:26 +0000264 sign_link = msg->trx->bts->oml_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000265 e1i_ts = sign_link->ts;
266 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
267 /* notify the driver we have something to write */
268 e1inp_driver = sign_link->ts->line->driver;
269 e1inp_driver->want_write(e1i_ts);
270 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000271 msgb_enqueue(&sign_link->tx_list, msg);
272
Holger Freyther0469cf62009-03-31 12:14:16 +0000273 /* dump it */
274 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
275
Harald Welte1fa60c82009-02-09 18:13:26 +0000276 return 0;
277}
278
279/* Timeslot */
280
281/* configure and initialize one e1inp_ts */
282int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
283 enum e1inp_ts_type type)
284{
285 ts->type = type;
286 ts->line = line;
287
288 switch (type) {
289 case E1INP_TS_TYPE_SIGN:
290 INIT_LLIST_HEAD(&ts->sign.sign_links);
291 break;
292 case E1INP_TS_TYPE_TRAU:
293 subchan_mux_init(&ts->trau.mux);
294 ts->trau.demux.out_cb = subch_cb;
295 ts->trau.demux.data = ts;
296 subch_demux_init(&ts->trau.demux);
297 break;
298 default:
299 fprintf(stderr, "unsupported E1 timeslot type %u\n",
300 ts->type);
301 return -EINVAL;
302 }
303 return 0;
304}
305
306static struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
307{
308 struct e1inp_line *e1i_line;
309
310 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000311 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000312 if (e1i_line->num == e1_nr)
313 return e1i_line;
314 }
315 return NULL;
316}
317
318static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
319{
320 struct e1inp_line *e1i_line;
321
322 e1i_line = e1inp_line_get(e1_nr);
323 if (!e1i_line)
324 return NULL;
325
326 return &e1i_line->ts[ts_nr-1];
327}
328
329struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
330{
331 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
332
333 if (!e1i_ts)
334 return NULL;
335
336 return &e1i_ts->trau.mux;
337}
338
339/* Signalling Link */
340
341struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
342 u_int8_t tei, u_int8_t sapi)
343{
344 struct e1inp_sign_link *link;
345
346 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
347 if (link->sapi == sapi && link->tei == tei)
348 return link;
349 }
350
351 return NULL;
352}
353
354/* create a new signalling link in a E1 timeslot */
355
356struct e1inp_sign_link *
357e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
358 struct gsm_bts_trx *trx, u_int8_t tei,
359 u_int8_t sapi)
360{
361 struct e1inp_sign_link *link;
362
363 if (ts->type != E1INP_TS_TYPE_SIGN)
364 return NULL;
365
366 link = malloc(sizeof(*link));
367 if (!link)
368 return NULL;
369
370 memset(link, 0, sizeof(*link));
371
372 link->ts = ts;
373 link->type = type;
374 INIT_LLIST_HEAD(&link->tx_list);
375 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000376 link->tei = tei;
377 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000378
379 llist_add_tail(&link->list, &ts->sign.sign_links);
380
381 return link;
382}
383
384/* the E1 driver tells us he has received something on a TS */
385int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
386 u_int8_t tei, u_int8_t sapi)
387{
388 struct e1inp_sign_link *link;
389 int ret;
390
391 switch (ts->type) {
392 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000393 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000394 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000395 link = e1inp_lookup_sign_link(ts, tei, sapi);
396 if (!link) {
397 fprintf(stderr, "didn't find singalling link for "
398 "tei %d, sapi %d\n", tei, sapi);
399 return -EINVAL;
400 }
401 switch (link->type) {
402 case E1INP_SIGN_OML:
403 msg->trx = link->trx;
404 ret = abis_nm_rcvmsg(msg);
405 break;
406 case E1INP_SIGN_RSL:
407 msg->trx = link->trx;
408 ret = abis_rsl_rcvmsg(msg);
409 break;
410 default:
411 ret = -EINVAL;
412 fprintf(stderr, "unknown link type %u\n", link->type);
413 break;
414 }
415 break;
416 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000417 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000418 break;
419 default:
420 ret = -EINVAL;
421 fprintf(stderr, "unknown TS type %u\n", ts->type);
422 break;
423 }
424
425 return ret;
426}
427
428#define TSX_ALLOC_SIZE 4096
429
430/* called by driver if it wants to transmit on a given TS */
431struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
432 struct e1inp_sign_link **sign_link)
433{
434 struct e1inp_sign_link *link;
435 struct msgb *msg = NULL;
436 int len;
437
438 switch (e1i_ts->type) {
439 case E1INP_TS_TYPE_SIGN:
440 /* FIXME: implement this round robin */
441 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
442 msg = msgb_dequeue(&link->tx_list);
443 if (msg) {
444 if (sign_link)
445 *sign_link = link;
446 break;
447 }
448 }
449 break;
450 case E1INP_TS_TYPE_TRAU:
451 msg = msgb_alloc(TSX_ALLOC_SIZE);
452 if (!msg)
453 return NULL;
454 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
455 msgb_put(msg, 40);
456 break;
457 default:
458 fprintf(stderr, "unsupported E1 TS type %u\n", e1i_ts->type);
459 return NULL;
460 }
461 return msg;
462}
463
464/* called by driver in case some kind of link state event */
465int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
466{
467 struct e1inp_sign_link *link;
468
469 link = e1inp_lookup_sign_link(ts, tei, sapi);
470 if (!link)
471 return -EINVAL;
472
473 /* FIXME: report further upwards */
Holger Freytherff9592f2009-03-09 16:17:14 +0000474 input_event(evt, link->type, link->trx);
475 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000476}
477
478/* register a driver with the E1 core */
479int e1inp_driver_register(struct e1inp_driver *drv)
480{
Harald Welte44d542e2009-03-10 18:24:35 +0000481 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000482 return 0;
483}
484
485/* register a line with the E1 core */
486int e1inp_line_register(struct e1inp_line *line)
487{
488 int i;
489
Harald Welted256d4f2009-03-10 19:44:48 +0000490 for (i = 0; i < NUM_E1_TS; i++) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000491 line->ts[i].num = i+1;
Harald Welted256d4f2009-03-10 19:44:48 +0000492 line->ts[i].line = line;
493 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000494
Harald Welte44d542e2009-03-10 18:24:35 +0000495 llist_add_tail(&line->list, &e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000496
497 return 0;
498}