blob: 5e0670b76e88054b9ddd772ec83f5638ffdc15a6 [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>
38#define AF_ISDN 34
39#define PF_ISDN AF_ISDN
40
41#include <openbsc/select.h>
42#include <openbsc/msgb.h>
43#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
45#include <openbsc/e1_input.h>
46#include <openbsc/abis_nm.h>
47#include <openbsc/abis_rsl.h>
48#include <openbsc/linuxlist.h>
49#include <openbsc/subchan_demux.h>
50#include <openbsc/trau_mux.h>
51
52#define NUM_E1_TS 32
53
54/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000055LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000056
57/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000058LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000059
Holger Freytherff9592f2009-03-09 16:17:14 +000060/* to be implemented, e.g. by bsc_hack.c */
61void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx);
62
Harald Welte1fa60c82009-02-09 18:13:26 +000063#if 0
64/*
65 * pcap writing of the misdn load
66 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
67 */
68#define DLT_LINUX_LAPD 177
69#define PCAP_INPUT 0
70#define PCAP_OUTPUT 1
71
72struct pcap_hdr {
73 u_int32_t magic_number;
74 u_int16_t version_major;
75 u_int16_t version_minor;
76 int32_t thiszone;
77 u_int32_t sigfigs;
78 u_int32_t snaplen;
79 u_int32_t network;
80} __attribute__((packed));
81
82struct pcaprec_hdr {
83 u_int32_t ts_sec;
84 u_int32_t ts_usec;
85 u_int32_t incl_len;
86 u_int32_t orig_len;
87} __attribute__((packed));
88
89struct fake_linux_lapd_header {
90 u_int16_t pkttype;
91 u_int16_t hatype;
92 u_int16_t halen;
93 u_int64_t addr;
94 int16_t protocol;
95} __attribute__((packed));
96
97struct lapd_header {
98 u_int8_t ea1 : 1;
99 u_int8_t cr : 1;
100 u_int8_t sapi : 6;
101 u_int8_t ea2 : 1;
102 u_int8_t tei : 7;
103 u_int8_t control_foo; /* fake UM's ... */
104} __attribute__((packed));
105
106static_assert((int)&((struct fake_linux_lapd_header*)NULL)->hatype == 2, hatype_offset);
107static_assert((int)&((struct fake_linux_lapd_header*)NULL)->halen == 4, halen_offset);
108static_assert((int)&((struct fake_linux_lapd_header*)NULL)->addr == 6, addr_offset);
109static_assert((int)&((struct fake_linux_lapd_header*)NULL)->protocol == 14, proto_offset);
110static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
111
112
113static int pcap_fd = -1;
114
115void mi_set_pcap_fd(int fd)
116{
117 int ret;
118 struct pcap_hdr header = {
119 .magic_number = 0xa1b2c3d4,
120 .version_major = 2,
121 .version_minor = 4,
122 .thiszone = 0,
123 .sigfigs = 0,
124 .snaplen = 65535,
125 .network = DLT_LINUX_LAPD,
126 };
127
128 pcap_fd = fd;
129 ret = write(pcap_fd, &header, sizeof(header));
130}
131
132/* This currently only works for the D-Channel */
133static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr,
134 struct msgb *msg) {
135 if (pcap_fd < 0)
136 return;
137
138 int ret;
139 time_t cur_time;
140 struct tm *tm;
141
142 struct fake_linux_lapd_header header = {
143 .pkttype = 4,
144 .hatype = 0,
145 .halen = 0,
146 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
147 .protocol = ntohs(48),
148 };
149
150 struct lapd_header lapd_header = {
151 .ea1 = 0,
152 .cr = direction == PCAP_OUTPUT ? 1 : 0,
153 .sapi = addr->sapi & 0x3F,
154 .ea2 = 1,
155 .tei = addr->tei & 0x7F,
156 .control_foo = 0x03 /* UI */,
157 };
158
159 struct pcaprec_hdr payload_header = {
160 .ts_sec = 0,
161 .ts_usec = 0,
162 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
163 + sizeof(struct lapd_header)
164 - MISDN_HEADER_LEN,
165 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
166 + sizeof(struct lapd_header)
167 - MISDN_HEADER_LEN,
168 };
169
170
171 cur_time = time(NULL);
172 tm = localtime(&cur_time);
173 payload_header.ts_sec = mktime(tm);
174
175 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
176 ret = write(pcap_fd, &header, sizeof(header));
177 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
178 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
179 msg->len - MISDN_HEADER_LEN);
180}
181#endif
182
183/* callback when a TRAU frame was received */
184static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
185 void *_priv)
186{
187 struct e1inp_ts *e1i_ts = _priv;
188 struct gsm_e1_subslot src_ss;
189
190 src_ss.e1_nr = e1i_ts->line->num;
191 src_ss.e1_ts = e1i_ts->num;
192 src_ss.e1_ts_ss = ch;
193
194 return trau_mux_input(&src_ss, data, len);
195}
196
197int abis_rsl_sendmsg(struct msgb *msg)
198{
199 struct e1inp_sign_link *sign_link;
200 struct e1inp_driver *e1inp_driver;
201
202 msg->l2h = msg->data;
203
204 if (!msg->trx || !msg->trx->rsl_link) {
205 fprintf(stderr, "rsl_sendmsg: msg->trx == NULL\n");
206 return -EINVAL;
207 }
208
209 sign_link = msg->trx->rsl_link;
210 msgb_enqueue(&sign_link->tx_list, msg);
211
212 /* notify the driver we have something to write */
213 e1inp_driver = sign_link->ts->line->driver;
214 e1inp_driver->want_write(sign_link->ts);
215
216 return 0;
217}
218
219int _abis_nm_sendmsg(struct msgb *msg)
220{
221 struct e1inp_sign_link *sign_link;
222 struct e1inp_driver *e1inp_driver;
223
224 msg->l2h = msg->data;
225
226 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Holger Freytherfb3f5192009-02-09 23:09:15 +0000227 fprintf(stderr, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000228 return -EINVAL;
229 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000230
Harald Welte1fa60c82009-02-09 18:13:26 +0000231 sign_link = msg->trx->bts->oml_link;
232 msgb_enqueue(&sign_link->tx_list, msg);
233
234 /* notify the driver we have something to write */
235 e1inp_driver = sign_link->ts->line->driver;
236 e1inp_driver->want_write(sign_link->ts);
237
238 return 0;
239}
240
241/* Timeslot */
242
243/* configure and initialize one e1inp_ts */
244int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
245 enum e1inp_ts_type type)
246{
247 ts->type = type;
248 ts->line = line;
249
250 switch (type) {
251 case E1INP_TS_TYPE_SIGN:
252 INIT_LLIST_HEAD(&ts->sign.sign_links);
253 break;
254 case E1INP_TS_TYPE_TRAU:
255 subchan_mux_init(&ts->trau.mux);
256 ts->trau.demux.out_cb = subch_cb;
257 ts->trau.demux.data = ts;
258 subch_demux_init(&ts->trau.demux);
259 break;
260 default:
261 fprintf(stderr, "unsupported E1 timeslot type %u\n",
262 ts->type);
263 return -EINVAL;
264 }
265 return 0;
266}
267
268static struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
269{
270 struct e1inp_line *e1i_line;
271
272 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000273 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000274 if (e1i_line->num == e1_nr)
275 return e1i_line;
276 }
277 return NULL;
278}
279
280static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
281{
282 struct e1inp_line *e1i_line;
283
284 e1i_line = e1inp_line_get(e1_nr);
285 if (!e1i_line)
286 return NULL;
287
288 return &e1i_line->ts[ts_nr-1];
289}
290
291struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
292{
293 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
294
295 if (!e1i_ts)
296 return NULL;
297
298 return &e1i_ts->trau.mux;
299}
300
301/* Signalling Link */
302
303struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
304 u_int8_t tei, u_int8_t sapi)
305{
306 struct e1inp_sign_link *link;
307
308 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
309 if (link->sapi == sapi && link->tei == tei)
310 return link;
311 }
312
313 return NULL;
314}
315
316/* create a new signalling link in a E1 timeslot */
317
318struct e1inp_sign_link *
319e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
320 struct gsm_bts_trx *trx, u_int8_t tei,
321 u_int8_t sapi)
322{
323 struct e1inp_sign_link *link;
324
325 if (ts->type != E1INP_TS_TYPE_SIGN)
326 return NULL;
327
328 link = malloc(sizeof(*link));
329 if (!link)
330 return NULL;
331
332 memset(link, 0, sizeof(*link));
333
334 link->ts = ts;
335 link->type = type;
336 INIT_LLIST_HEAD(&link->tx_list);
337 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000338 link->tei = tei;
339 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000340
341 llist_add_tail(&link->list, &ts->sign.sign_links);
342
343 return link;
344}
345
346/* the E1 driver tells us he has received something on a TS */
347int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
348 u_int8_t tei, u_int8_t sapi)
349{
350 struct e1inp_sign_link *link;
351 int ret;
352
353 switch (ts->type) {
354 case E1INP_TS_TYPE_SIGN:
355 /* FIXME: write pcap packet */
356 /* consult the list of signalling links */
357 link = e1inp_lookup_sign_link(ts, tei, sapi);
358 if (!link) {
359 fprintf(stderr, "didn't find singalling link for "
360 "tei %d, sapi %d\n", tei, sapi);
361 return -EINVAL;
362 }
363 switch (link->type) {
364 case E1INP_SIGN_OML:
365 msg->trx = link->trx;
366 ret = abis_nm_rcvmsg(msg);
367 break;
368 case E1INP_SIGN_RSL:
369 msg->trx = link->trx;
370 ret = abis_rsl_rcvmsg(msg);
371 break;
372 default:
373 ret = -EINVAL;
374 fprintf(stderr, "unknown link type %u\n", link->type);
375 break;
376 }
377 break;
378 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000379 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000380 break;
381 default:
382 ret = -EINVAL;
383 fprintf(stderr, "unknown TS type %u\n", ts->type);
384 break;
385 }
386
387 return ret;
388}
389
390#define TSX_ALLOC_SIZE 4096
391
392/* called by driver if it wants to transmit on a given TS */
393struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
394 struct e1inp_sign_link **sign_link)
395{
396 struct e1inp_sign_link *link;
397 struct msgb *msg = NULL;
398 int len;
399
400 switch (e1i_ts->type) {
401 case E1INP_TS_TYPE_SIGN:
402 /* FIXME: implement this round robin */
403 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
404 msg = msgb_dequeue(&link->tx_list);
405 if (msg) {
406 if (sign_link)
407 *sign_link = link;
408 break;
409 }
410 }
411 break;
412 case E1INP_TS_TYPE_TRAU:
413 msg = msgb_alloc(TSX_ALLOC_SIZE);
414 if (!msg)
415 return NULL;
416 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
417 msgb_put(msg, 40);
418 break;
419 default:
420 fprintf(stderr, "unsupported E1 TS type %u\n", e1i_ts->type);
421 return NULL;
422 }
423 return msg;
424}
425
426/* called by driver in case some kind of link state event */
427int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
428{
429 struct e1inp_sign_link *link;
430
431 link = e1inp_lookup_sign_link(ts, tei, sapi);
432 if (!link)
433 return -EINVAL;
434
435 /* FIXME: report further upwards */
Holger Freytherff9592f2009-03-09 16:17:14 +0000436 input_event(evt, link->type, link->trx);
437 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000438}
439
440/* register a driver with the E1 core */
441int e1inp_driver_register(struct e1inp_driver *drv)
442{
Harald Welte44d542e2009-03-10 18:24:35 +0000443 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000444 return 0;
445}
446
447/* register a line with the E1 core */
448int e1inp_line_register(struct e1inp_line *line)
449{
450 int i;
451
452 for (i = 0; i < NUM_E1_TS; i++)
453 line->ts[i].num = i+1;
454
Harald Welte44d542e2009-03-10 18:24:35 +0000455 llist_add_tail(&line->list, &e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000456
457 return 0;
458}