blob: 153a4137ca3640c7de60faa1a04bb4bb2a1050fd [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#if 0
66/*
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
117void mi_set_pcap_fd(int fd)
118{
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 */
135static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr,
136 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,
155 .sapi = addr->sapi & 0x3F,
156 .ea2 = 1,
157 .tei = addr->tei & 0x7F,
158 .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}
183#endif
184
Harald Welted256d4f2009-03-10 19:44:48 +0000185static const char *sign_types[] = {
186 [E1INP_SIGN_NONE] = "None",
187 [E1INP_SIGN_OML] = "OML",
188 [E1INP_SIGN_RSL] = "RSL",
189};
190const char *e1inp_signtype_name(enum e1inp_sign_type tp)
191{
192 if (tp >= ARRAY_SIZE(sign_types))
193 return "undefined";
194 return sign_types[tp];
195}
196
197static const char *ts_types[] = {
198 [E1INP_TS_TYPE_NONE] = "None",
199 [E1INP_TS_TYPE_SIGN] = "Signalling",
200 [E1INP_TS_TYPE_TRAU] = "TRAU",
201};
202
203const char *e1inp_tstype_name(enum e1inp_ts_type tp)
204{
205 if (tp >= ARRAY_SIZE(ts_types))
206 return "undefined";
207 return ts_types[tp];
208}
209
Harald Welte1fa60c82009-02-09 18:13:26 +0000210/* callback when a TRAU frame was received */
211static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
212 void *_priv)
213{
214 struct e1inp_ts *e1i_ts = _priv;
215 struct gsm_e1_subslot src_ss;
216
217 src_ss.e1_nr = e1i_ts->line->num;
218 src_ss.e1_ts = e1i_ts->num;
219 src_ss.e1_ts_ss = ch;
220
221 return trau_mux_input(&src_ss, data, len);
222}
223
224int abis_rsl_sendmsg(struct msgb *msg)
225{
226 struct e1inp_sign_link *sign_link;
227 struct e1inp_driver *e1inp_driver;
228
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;
237 msgb_enqueue(&sign_link->tx_list, msg);
238
239 /* notify the driver we have something to write */
240 e1inp_driver = sign_link->ts->line->driver;
241 e1inp_driver->want_write(sign_link->ts);
242
243 return 0;
244}
245
246int _abis_nm_sendmsg(struct msgb *msg)
247{
248 struct e1inp_sign_link *sign_link;
249 struct e1inp_driver *e1inp_driver;
250
251 msg->l2h = msg->data;
252
253 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Holger Freytherfb3f5192009-02-09 23:09:15 +0000254 fprintf(stderr, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000255 return -EINVAL;
256 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000257
Harald Welte1fa60c82009-02-09 18:13:26 +0000258 sign_link = msg->trx->bts->oml_link;
259 msgb_enqueue(&sign_link->tx_list, msg);
260
261 /* notify the driver we have something to write */
262 e1inp_driver = sign_link->ts->line->driver;
263 e1inp_driver->want_write(sign_link->ts);
264
265 return 0;
266}
267
268/* Timeslot */
269
270/* configure and initialize one e1inp_ts */
271int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
272 enum e1inp_ts_type type)
273{
274 ts->type = type;
275 ts->line = line;
276
277 switch (type) {
278 case E1INP_TS_TYPE_SIGN:
279 INIT_LLIST_HEAD(&ts->sign.sign_links);
280 break;
281 case E1INP_TS_TYPE_TRAU:
282 subchan_mux_init(&ts->trau.mux);
283 ts->trau.demux.out_cb = subch_cb;
284 ts->trau.demux.data = ts;
285 subch_demux_init(&ts->trau.demux);
286 break;
287 default:
288 fprintf(stderr, "unsupported E1 timeslot type %u\n",
289 ts->type);
290 return -EINVAL;
291 }
292 return 0;
293}
294
295static struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
296{
297 struct e1inp_line *e1i_line;
298
299 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000300 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000301 if (e1i_line->num == e1_nr)
302 return e1i_line;
303 }
304 return NULL;
305}
306
307static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
308{
309 struct e1inp_line *e1i_line;
310
311 e1i_line = e1inp_line_get(e1_nr);
312 if (!e1i_line)
313 return NULL;
314
315 return &e1i_line->ts[ts_nr-1];
316}
317
318struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
319{
320 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
321
322 if (!e1i_ts)
323 return NULL;
324
325 return &e1i_ts->trau.mux;
326}
327
328/* Signalling Link */
329
330struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
331 u_int8_t tei, u_int8_t sapi)
332{
333 struct e1inp_sign_link *link;
334
335 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
336 if (link->sapi == sapi && link->tei == tei)
337 return link;
338 }
339
340 return NULL;
341}
342
343/* create a new signalling link in a E1 timeslot */
344
345struct e1inp_sign_link *
346e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
347 struct gsm_bts_trx *trx, u_int8_t tei,
348 u_int8_t sapi)
349{
350 struct e1inp_sign_link *link;
351
352 if (ts->type != E1INP_TS_TYPE_SIGN)
353 return NULL;
354
355 link = malloc(sizeof(*link));
356 if (!link)
357 return NULL;
358
359 memset(link, 0, sizeof(*link));
360
361 link->ts = ts;
362 link->type = type;
363 INIT_LLIST_HEAD(&link->tx_list);
364 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000365 link->tei = tei;
366 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000367
368 llist_add_tail(&link->list, &ts->sign.sign_links);
369
370 return link;
371}
372
373/* the E1 driver tells us he has received something on a TS */
374int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
375 u_int8_t tei, u_int8_t sapi)
376{
377 struct e1inp_sign_link *link;
378 int ret;
379
380 switch (ts->type) {
381 case E1INP_TS_TYPE_SIGN:
382 /* FIXME: write pcap packet */
383 /* consult the list of signalling links */
384 link = e1inp_lookup_sign_link(ts, tei, sapi);
385 if (!link) {
386 fprintf(stderr, "didn't find singalling link for "
387 "tei %d, sapi %d\n", tei, sapi);
388 return -EINVAL;
389 }
390 switch (link->type) {
391 case E1INP_SIGN_OML:
392 msg->trx = link->trx;
393 ret = abis_nm_rcvmsg(msg);
394 break;
395 case E1INP_SIGN_RSL:
396 msg->trx = link->trx;
397 ret = abis_rsl_rcvmsg(msg);
398 break;
399 default:
400 ret = -EINVAL;
401 fprintf(stderr, "unknown link type %u\n", link->type);
402 break;
403 }
404 break;
405 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000406 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000407 break;
408 default:
409 ret = -EINVAL;
410 fprintf(stderr, "unknown TS type %u\n", ts->type);
411 break;
412 }
413
414 return ret;
415}
416
417#define TSX_ALLOC_SIZE 4096
418
419/* called by driver if it wants to transmit on a given TS */
420struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
421 struct e1inp_sign_link **sign_link)
422{
423 struct e1inp_sign_link *link;
424 struct msgb *msg = NULL;
425 int len;
426
427 switch (e1i_ts->type) {
428 case E1INP_TS_TYPE_SIGN:
429 /* FIXME: implement this round robin */
430 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
431 msg = msgb_dequeue(&link->tx_list);
432 if (msg) {
433 if (sign_link)
434 *sign_link = link;
435 break;
436 }
437 }
438 break;
439 case E1INP_TS_TYPE_TRAU:
440 msg = msgb_alloc(TSX_ALLOC_SIZE);
441 if (!msg)
442 return NULL;
443 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
444 msgb_put(msg, 40);
445 break;
446 default:
447 fprintf(stderr, "unsupported E1 TS type %u\n", e1i_ts->type);
448 return NULL;
449 }
450 return msg;
451}
452
453/* called by driver in case some kind of link state event */
454int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
455{
456 struct e1inp_sign_link *link;
457
458 link = e1inp_lookup_sign_link(ts, tei, sapi);
459 if (!link)
460 return -EINVAL;
461
462 /* FIXME: report further upwards */
Holger Freytherff9592f2009-03-09 16:17:14 +0000463 input_event(evt, link->type, link->trx);
464 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000465}
466
467/* register a driver with the E1 core */
468int e1inp_driver_register(struct e1inp_driver *drv)
469{
Harald Welte44d542e2009-03-10 18:24:35 +0000470 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000471 return 0;
472}
473
474/* register a line with the E1 core */
475int e1inp_line_register(struct e1inp_line *line)
476{
477 int i;
478
Harald Welted256d4f2009-03-10 19:44:48 +0000479 for (i = 0; i < NUM_E1_TS; i++) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000480 line->ts[i].num = i+1;
Harald Welted256d4f2009-03-10 19:44:48 +0000481 line->ts[i].line = line;
482 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000483
Harald Welte44d542e2009-03-10 18:24:35 +0000484 llist_add_tail(&line->list, &e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000485
486 return 0;
487}