blob: 14352d03e1c1222df05e103c39a37e5e0ba6c9de [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;
226
227 msg->l2h = msg->data;
228
229 if (!msg->trx || !msg->trx->rsl_link) {
230 fprintf(stderr, "rsl_sendmsg: msg->trx == NULL\n");
231 return -EINVAL;
232 }
233
234 sign_link = msg->trx->rsl_link;
235 msgb_enqueue(&sign_link->tx_list, msg);
236
Holger Freyther0469cf62009-03-31 12:14:16 +0000237 /* dump it */
238 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
239
Harald Welte1fa60c82009-02-09 18:13:26 +0000240 /* notify the driver we have something to write */
241 e1inp_driver = sign_link->ts->line->driver;
242 e1inp_driver->want_write(sign_link->ts);
243
244 return 0;
245}
246
247int _abis_nm_sendmsg(struct msgb *msg)
248{
249 struct e1inp_sign_link *sign_link;
250 struct e1inp_driver *e1inp_driver;
251
252 msg->l2h = msg->data;
253
254 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Holger Freytherfb3f5192009-02-09 23:09:15 +0000255 fprintf(stderr, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000256 return -EINVAL;
257 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000258
Harald Welte1fa60c82009-02-09 18:13:26 +0000259 sign_link = msg->trx->bts->oml_link;
260 msgb_enqueue(&sign_link->tx_list, msg);
261
Holger Freyther0469cf62009-03-31 12:14:16 +0000262 /* dump it */
263 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
264
Harald Welte1fa60c82009-02-09 18:13:26 +0000265 /* notify the driver we have something to write */
266 e1inp_driver = sign_link->ts->line->driver;
267 e1inp_driver->want_write(sign_link->ts);
268
269 return 0;
270}
271
272/* Timeslot */
273
274/* configure and initialize one e1inp_ts */
275int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
276 enum e1inp_ts_type type)
277{
278 ts->type = type;
279 ts->line = line;
280
281 switch (type) {
282 case E1INP_TS_TYPE_SIGN:
283 INIT_LLIST_HEAD(&ts->sign.sign_links);
284 break;
285 case E1INP_TS_TYPE_TRAU:
286 subchan_mux_init(&ts->trau.mux);
287 ts->trau.demux.out_cb = subch_cb;
288 ts->trau.demux.data = ts;
289 subch_demux_init(&ts->trau.demux);
290 break;
291 default:
292 fprintf(stderr, "unsupported E1 timeslot type %u\n",
293 ts->type);
294 return -EINVAL;
295 }
296 return 0;
297}
298
299static struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
300{
301 struct e1inp_line *e1i_line;
302
303 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000304 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000305 if (e1i_line->num == e1_nr)
306 return e1i_line;
307 }
308 return NULL;
309}
310
311static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
312{
313 struct e1inp_line *e1i_line;
314
315 e1i_line = e1inp_line_get(e1_nr);
316 if (!e1i_line)
317 return NULL;
318
319 return &e1i_line->ts[ts_nr-1];
320}
321
322struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
323{
324 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
325
326 if (!e1i_ts)
327 return NULL;
328
329 return &e1i_ts->trau.mux;
330}
331
332/* Signalling Link */
333
334struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
335 u_int8_t tei, u_int8_t sapi)
336{
337 struct e1inp_sign_link *link;
338
339 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
340 if (link->sapi == sapi && link->tei == tei)
341 return link;
342 }
343
344 return NULL;
345}
346
347/* create a new signalling link in a E1 timeslot */
348
349struct e1inp_sign_link *
350e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
351 struct gsm_bts_trx *trx, u_int8_t tei,
352 u_int8_t sapi)
353{
354 struct e1inp_sign_link *link;
355
356 if (ts->type != E1INP_TS_TYPE_SIGN)
357 return NULL;
358
359 link = malloc(sizeof(*link));
360 if (!link)
361 return NULL;
362
363 memset(link, 0, sizeof(*link));
364
365 link->ts = ts;
366 link->type = type;
367 INIT_LLIST_HEAD(&link->tx_list);
368 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000369 link->tei = tei;
370 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000371
372 llist_add_tail(&link->list, &ts->sign.sign_links);
373
374 return link;
375}
376
377/* the E1 driver tells us he has received something on a TS */
378int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
379 u_int8_t tei, u_int8_t sapi)
380{
381 struct e1inp_sign_link *link;
382 int ret;
383
384 switch (ts->type) {
385 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000386 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000387 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000388 link = e1inp_lookup_sign_link(ts, tei, sapi);
389 if (!link) {
390 fprintf(stderr, "didn't find singalling link for "
391 "tei %d, sapi %d\n", tei, sapi);
392 return -EINVAL;
393 }
394 switch (link->type) {
395 case E1INP_SIGN_OML:
396 msg->trx = link->trx;
397 ret = abis_nm_rcvmsg(msg);
398 break;
399 case E1INP_SIGN_RSL:
400 msg->trx = link->trx;
401 ret = abis_rsl_rcvmsg(msg);
402 break;
403 default:
404 ret = -EINVAL;
405 fprintf(stderr, "unknown link type %u\n", link->type);
406 break;
407 }
408 break;
409 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000410 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000411 break;
412 default:
413 ret = -EINVAL;
414 fprintf(stderr, "unknown TS type %u\n", ts->type);
415 break;
416 }
417
418 return ret;
419}
420
421#define TSX_ALLOC_SIZE 4096
422
423/* called by driver if it wants to transmit on a given TS */
424struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
425 struct e1inp_sign_link **sign_link)
426{
427 struct e1inp_sign_link *link;
428 struct msgb *msg = NULL;
429 int len;
430
431 switch (e1i_ts->type) {
432 case E1INP_TS_TYPE_SIGN:
433 /* FIXME: implement this round robin */
434 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
435 msg = msgb_dequeue(&link->tx_list);
436 if (msg) {
437 if (sign_link)
438 *sign_link = link;
439 break;
440 }
441 }
442 break;
443 case E1INP_TS_TYPE_TRAU:
444 msg = msgb_alloc(TSX_ALLOC_SIZE);
445 if (!msg)
446 return NULL;
447 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
448 msgb_put(msg, 40);
449 break;
450 default:
451 fprintf(stderr, "unsupported E1 TS type %u\n", e1i_ts->type);
452 return NULL;
453 }
454 return msg;
455}
456
457/* called by driver in case some kind of link state event */
458int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
459{
460 struct e1inp_sign_link *link;
461
462 link = e1inp_lookup_sign_link(ts, tei, sapi);
463 if (!link)
464 return -EINVAL;
465
466 /* FIXME: report further upwards */
Holger Freytherff9592f2009-03-09 16:17:14 +0000467 input_event(evt, link->type, link->trx);
468 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000469}
470
471/* register a driver with the E1 core */
472int e1inp_driver_register(struct e1inp_driver *drv)
473{
Harald Welte44d542e2009-03-10 18:24:35 +0000474 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000475 return 0;
476}
477
478/* register a line with the E1 core */
479int e1inp_line_register(struct e1inp_line *line)
480{
481 int i;
482
Harald Welted256d4f2009-03-10 19:44:48 +0000483 for (i = 0; i < NUM_E1_TS; i++) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000484 line->ts[i].num = i+1;
Harald Welted256d4f2009-03-10 19:44:48 +0000485 line->ts[i].line = line;
486 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000487
Harald Welte44d542e2009-03-10 18:24:35 +0000488 llist_add_tail(&line->list, &e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000489
490 return 0;
491}