blob: 245df50d8fe09fb9b542decf2688c3dd37d2e7d3 [file] [log] [blame]
Harald Welte1fa60c82009-02-09 18:13:26 +00001/* OpenBSC Abis input driver for mISDNuser */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <string.h>
29#include <time.h>
30#include <sys/fcntl.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35#include <mISDNif.h>
36
37//#define AF_COMPATIBILITY_FUNC
38//#include <compat_af_isdn.h>
39#define AF_ISDN 34
40#define PF_ISDN AF_ISDN
41
42#include <openbsc/select.h>
43#include <openbsc/msgb.h>
44#include <openbsc/debug.h>
45#include <openbsc/gsm_data.h>
46#include <openbsc/abis_nm.h>
47#include <openbsc/abis_rsl.h>
48#include <openbsc/subchan_demux.h>
49#include <openbsc/e1_input.h>
50
51/* data structure for one E1 interface with A-bis */
52struct mi_e1_handle {
53 /* The mISDN card number of the card we use */
54 int cardnr;
55};
56
57#define TS1_ALLOC_SIZE 300
58
59struct prim_name {
60 unsigned int prim;
61 const char *name;
62};
63
64const struct prim_name prim_names[] = {
Holger Freythercbc7b062009-02-09 23:10:48 +000065 { PH_CONTROL_IND, "PH_CONTROL_IND" },
66 { PH_DATA_IND, "PH_DATA_IND" },
67 { PH_DATA_CNF, "PH_DATA_CNF" },
Holger Freyther5d7e5572009-02-10 18:40:45 +000068 { PH_ACTIVATE_IND, "PH_ACTIVATE_IND" },
Harald Welte1fa60c82009-02-09 18:13:26 +000069 { DL_ESTABLISH_IND, "DL_ESTABLISH_IND" },
70 { DL_ESTABLISH_CNF, "DL_ESTABLISH_CNF" },
71 { DL_RELEASE_IND, "DL_RELEASE_IND" },
72 { DL_RELEASE_CNF, "DL_RELEASE_CNF" },
73 { DL_DATA_IND, "DL_DATA_IND" },
74 { DL_UNITDATA_IND, "DL_UNITDATA_IND" },
75 { DL_INFORMATION_IND, "DL_INFORMATION_IND" },
76 { MPH_ACTIVATE_IND, "MPH_ACTIVATE_IND" },
77 { MPH_DEACTIVATE_IND, "MPH_DEACTIVATE_IND" },
78};
79
80const char *get_prim_name(unsigned int prim)
81{
82 int i;
83
84 for (i = 0; i < ARRAY_SIZE(prim_names); i++) {
85 if (prim_names[i].prim == prim)
86 return prim_names[i].name;
87 }
88
89 return "UNKNOWN";
90}
91
92static int handle_ts1_read(struct bsc_fd *bfd)
93{
94 struct e1inp_line *line = bfd->data;
95 unsigned int ts_nr = bfd->priv_nr;
96 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
97 struct e1inp_sign_link *link;
98 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
99 struct sockaddr_mISDN l2addr;
100 struct mISDNhead *hh;
101 socklen_t alen;
102 int ret;
103
104 if (!msg)
105 return -ENOMEM;
106
107 hh = (struct mISDNhead *) msg->data;
108
109 alen = sizeof(l2addr);
110 ret = recvfrom(bfd->fd, msg->data, 300, 0,
111 (struct sockaddr *) &l2addr, &alen);
112 if (ret < 0) {
113 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
114 return ret;
115 }
116
117 if (alen != sizeof(l2addr))
118 return -EINVAL;
119
120 msgb_put(msg, ret);
121
122 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
123 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
124
Holger Freyther57dd7bf2009-02-10 01:04:22 +0000125 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x): %s\n",
126 ret, hh->prim, hh->id, get_prim_name(hh->prim));
Harald Welte1fa60c82009-02-09 18:13:26 +0000127
128 switch (hh->prim) {
129 case DL_INFORMATION_IND:
130 /* mISDN tells us which channel number is allocated for this
131 * tuple of (SAPI, TEI). */
132 DEBUGP(DMI, "use channel(%d) sapi(%d) tei(%d) for now\n",
133 l2addr.channel, l2addr.sapi, l2addr.tei);
134 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
135 if (!link) {
136 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
137 free(msg);
138 return -EINVAL;
139 }
140 /* save the channel number in the driver private struct */
141 link->driver.misdn.channel = l2addr.channel;
142 break;
143 case MPH_ACTIVATE_IND:
144 ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, l2addr.tei, l2addr.sapi);
145 break;
146 case MPH_DEACTIVATE_IND:
147 ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, l2addr.tei, l2addr.sapi);
148 break;
149 case DL_DATA_IND:
Harald Welte1fa60c82009-02-09 18:13:26 +0000150 msg->l2h = msg->data + MISDN_HEADER_LEN;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000151 DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
Harald Welte1fa60c82009-02-09 18:13:26 +0000152 ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
153 break;
154 default:
155 break;
156 }
157 return ret;
158}
159
160static int handle_ts1_write(struct bsc_fd *bfd)
161{
162 struct e1inp_line *line = bfd->data;
163 unsigned int ts_nr = bfd->priv_nr;
164 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
165 struct e1inp_sign_link *sign_link;
166 struct sockaddr_mISDN sa;
167 struct msgb *msg;
168 struct mISDNhead *hh;
169 u_int8_t *l2_data;
170 int ret;
171
172 /* get the next msg for this timeslot */
173 msg = e1inp_tx_ts(e1i_ts, &sign_link);
174 if (!msg) {
175 bfd->when &= ~BSC_FD_WRITE;
176 return 0;
177 }
178
179 l2_data = msg->data;
180
181 /* prepend the mISDNhead */
182 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
183 hh->prim = DL_DATA_REQ;
184
Harald Welte3cc4bf52009-02-28 13:08:01 +0000185 DEBUGP(DMI, "TX TEI(%d): %s\n", sign_link->tei,
186 hexdump(l2_data, msg->len - MISDN_HEADER_LEN));
Harald Welte1fa60c82009-02-09 18:13:26 +0000187
188 /* construct the sockaddr */
189 sa.family = AF_ISDN;
190 sa.sapi = sign_link->sapi;
191 sa.dev = sign_link->tei;
192 sa.channel = sign_link->driver.misdn.channel;
193
194 ret = sendto(bfd->fd, msg->data, msg->len, 0,
195 (struct sockaddr *)&sa, sizeof(sa));
196 msgb_free(msg);
197
198 /* FIXME: this has to go */
199 usleep(100000);
200
201 return ret;
202}
203
Harald Weltef9227c72009-02-18 03:39:00 +0000204#define BCHAN_TX_GRAN 160
Harald Welte8ffcfed2009-02-10 18:18:50 +0000205/* write to a B channel TS */
206static int handle_tsX_write(struct bsc_fd *bfd)
207{
208 struct e1inp_line *line = bfd->data;
209 unsigned int ts_nr = bfd->priv_nr;
210 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
211 struct mISDNhead *hh;
212 u_int8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)];
213 struct subch_mux *mx = &e1i_ts->trau.mux;
214 int ret;
Harald Welte1fa60c82009-02-09 18:13:26 +0000215
Harald Welte8ffcfed2009-02-10 18:18:50 +0000216 hh = (struct mISDNhead *) tx_buf;
217 hh->prim = PH_DATA_REQ;
218
219 subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
220
Harald Welte3cc4bf52009-02-28 13:08:01 +0000221 DEBUGP(DMIB, "BCHAN TX: %s\n",
222 hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN));
Harald Welte8ffcfed2009-02-10 18:18:50 +0000223
224 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
225 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
226 DEBUGP(DMIB, "send returns %d instead of %u\n", ret,
227 sizeof(*hh) + BCHAN_TX_GRAN);
228
229 return ret;
230}
231
232#define TSX_ALLOC_SIZE 4096
Harald Welte1fa60c82009-02-09 18:13:26 +0000233/* FIXME: read from a B channel TS */
234static int handle_tsX_read(struct bsc_fd *bfd)
235{
236 struct e1inp_line *line = bfd->data;
237 unsigned int ts_nr = bfd->priv_nr;
238 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
239 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
240 struct mISDNhead *hh;
241 int ret;
242
243 if (!msg)
244 return -ENOMEM;
245
246 hh = (struct mISDNhead *) msg->data;
247
248 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
249 if (ret < 0) {
250 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
251 return ret;
252 }
253
254 msgb_put(msg, ret);
255
256 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
257 ret, hh->prim, hh->id, get_prim_name(hh->prim));
258
259 switch (hh->prim) {
260 case PH_DATA_IND:
261 msg->l2h = msg->data + MISDN_HEADER_LEN;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000262 DEBUGP(DMIB, "BCHAN RX: %s\n",
263 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
Harald Welte1fa60c82009-02-09 18:13:26 +0000264 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
265 break;
Holger Freyther5d7e5572009-02-10 18:40:45 +0000266 case PH_ACTIVATE_IND:
Harald Welte8ffcfed2009-02-10 18:18:50 +0000267 case PH_DATA_CNF:
268 /* physical layer indicates that data has been sent,
269 * we thus can send some more data */
270 ret = handle_tsX_write(bfd);
Harald Welte1fa60c82009-02-09 18:13:26 +0000271 default:
272 break;
273 }
274 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
275 msgb_free(msg);
276
277 return ret;
278}
279
Harald Welte1fa60c82009-02-09 18:13:26 +0000280/* callback from select.c in case one of the fd's can be read/written */
281static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
282{
283 struct e1inp_line *line = bfd->data;
284 unsigned int ts_nr = bfd->priv_nr;
285 unsigned int idx = ts_nr-1;
286 struct e1inp_ts *e1i_ts = &line->ts[idx];
287 int rc = 0;
288
289 switch (e1i_ts->type) {
290 case E1INP_TS_TYPE_SIGN:
291 if (what & BSC_FD_READ)
292 rc = handle_ts1_read(bfd);
293 if (what & BSC_FD_WRITE)
294 rc = handle_ts1_write(bfd);
295 break;
296 case E1INP_TS_TYPE_TRAU:
297 if (what & BSC_FD_READ)
298 rc = handle_tsX_read(bfd);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000299 /* We never include the mISDN B-Channel FD into the
300 * writeset, since it doesn't support poll() based
301 * write flow control */
Harald Welte1fa60c82009-02-09 18:13:26 +0000302 break;
303 default:
304 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
305 break;
306 }
307
308 return rc;
309}
310
311static int activate_bchan(struct e1inp_line *line, int ts, int act)
312{
313 struct mISDNhead hh;
314 int ret;
315 unsigned int idx = ts-1;
316 struct e1inp_ts *e1i_ts = &line->ts[idx];
317 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
318
319 fprintf(stdout, "activate bchan\n");
320 if (act)
321 hh.prim = PH_ACTIVATE_REQ;
322 else
323 hh.prim = PH_DEACTIVATE_REQ;
324
325 hh.id = MISDN_ID_ANY;
326 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
327 if (ret < 0) {
328 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
329 strerror(errno));
330 }
331
332 return ret;
333}
334
335static int ts_want_write(struct e1inp_ts *e1i_ts)
336{
Harald Welte8ffcfed2009-02-10 18:18:50 +0000337 /* We never include the mISDN B-Channel FD into the
338 * writeset, since it doesn't support poll() based
339 * write flow control */
340 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
341 return 0;
342
Harald Welte1fa60c82009-02-09 18:13:26 +0000343 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
344
345 return 0;
346}
347
348struct e1inp_driver misdn_driver = {
349 .name = "mISDNuser",
350 .want_write = ts_want_write,
351};
352
353static int mi_e1_setup(struct e1inp_line *line)
354{
355 struct mi_e1_handle *e1h = line->driver_data;
356 int ts, ret;
357
358 /* TS0 is CRC4, don't need any fd for it */
359 for (ts = 1; ts < NUM_E1_TS; ts++) {
360 unsigned int idx = ts-1;
361 struct e1inp_ts *e1i_ts = &line->ts[idx];
362 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
363 struct sockaddr_mISDN addr;
364
365 bfd->data = line;
366 bfd->priv_nr = ts;
367 bfd->cb = misdn_fd_cb;
368
369 switch (e1i_ts->type) {
370 case E1INP_TS_TYPE_NONE:
371 continue;
372 break;
373 case E1INP_TS_TYPE_SIGN:
374 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
375 bfd->when = BSC_FD_READ;
376 break;
377 case E1INP_TS_TYPE_TRAU:
378 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000379 /* We never include the mISDN B-Channel FD into the
380 * writeset, since it doesn't support poll() based
381 * write flow control */
382 bfd->when = BSC_FD_READ;
Harald Welte1fa60c82009-02-09 18:13:26 +0000383 break;
384 }
385
386 if (bfd->fd < 0) {
387 fprintf(stderr, "could not open socket %s\n",
388 strerror(errno));
389 return bfd->fd;
390 }
391
392 memset(&addr, 0, sizeof(addr));
393 addr.family = AF_ISDN;
394 addr.dev = e1h->cardnr;
395 switch (e1i_ts->type) {
396 case E1INP_TS_TYPE_SIGN:
397 addr.channel = 0;
398 /* SAPI not supported yet in kernel */
399 //addr.sapi = e1inp_ts->sign.sapi;
400 addr.sapi = 0;
401 addr.tei = GROUP_TEI;
402 break;
403 case E1INP_TS_TYPE_TRAU:
404 addr.channel = ts;
405 break;
406 default:
407 DEBUGP(DMI, "unsupported E1 TS type: %u\n",
408 e1i_ts->type);
409 break;
410 }
411
412 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
413 if (ret < 0) {
414 fprintf(stderr, "could not bind l2 socket %s\n",
415 strerror(errno));
416 return -EIO;
417 }
418
419 /* FIXME: only activate B-Channels once we start to
420 * use them to conserve CPU power */
421 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
422 activate_bchan(line, ts, 1);
423
424 ret = bsc_register_fd(bfd);
425 if (ret < 0) {
426 fprintf(stderr, "could not register FD: %s\n",
427 strerror(ret));
428 return ret;
429 }
430 }
431
432 return 0;
433}
434
435int mi_setup(int cardnr, struct e1inp_line *line)
436{
437 struct mi_e1_handle *e1h;
438 int sk, ret, cnt;
439 struct mISDN_devinfo devinfo;
440
441 /* register the driver with the core */
442 /* FIXME: do this in the plugin initializer function */
443 ret = e1inp_driver_register(&misdn_driver);
444 if (ret)
445 return ret;
446
447 /* create the actual line instance */
448 /* FIXME: do this independent of driver registration */
449 e1h = malloc(sizeof(*e1h));
450 memset(e1h, 0, sizeof(*e1h));
451
452 e1h->cardnr = cardnr;
453
454 line->driver = &misdn_driver;
455 line->driver_data = e1h;
456
457 /* open the ISDN card device */
458 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
459 if (sk < 0) {
460 fprintf(stderr, "could not open socket %s\n", strerror(errno));
461 return sk;
462 }
463
464 ret = ioctl(sk, IMGETCOUNT, &cnt);
465 if (ret) {
466 fprintf(stderr, "error getting interf count: %s\n",
467 strerror(errno));
468 close(sk);
469 return -ENODEV;
470 }
471 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
472 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
473#if 1
474 devinfo.id = e1h->cardnr;
475 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
476 if (ret < 0) {
477 fprintf(stdout, "error getting info for device %d: %s\n",
478 e1h->cardnr, strerror(errno));
479 return -ENODEV;
480 }
481 fprintf(stdout, " id: %d\n", devinfo.id);
482 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
483 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
484 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
485 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
486 fprintf(stdout, " name: %s\n", devinfo.name);
487#endif
488
489 ret = mi_e1_setup(line);
490 if (ret)
491 return ret;
492
493 return e1inp_line_register(line);
494}