blob: 6b3cb6f084fc57129d26833d82a53093b7e3cdb8 [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
Holger Freytheracb688b2009-05-01 04:59:55 +0000117 if (alen != sizeof(l2addr)) {
118 fprintf(stderr, "%s error len\n", __func__);
Harald Welte1fa60c82009-02-09 18:13:26 +0000119 return -EINVAL;
Holger Freytheracb688b2009-05-01 04:59:55 +0000120 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000121
122 msgb_put(msg, ret);
123
124 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
125 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
126
Holger Freyther57dd7bf2009-02-10 01:04:22 +0000127 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x): %s\n",
128 ret, hh->prim, hh->id, get_prim_name(hh->prim));
Harald Welte1fa60c82009-02-09 18:13:26 +0000129
130 switch (hh->prim) {
131 case DL_INFORMATION_IND:
132 /* mISDN tells us which channel number is allocated for this
133 * tuple of (SAPI, TEI). */
Holger Freytheracb688b2009-05-01 04:59:55 +0000134 DEBUGP(DMI, "DL_INFORMATION_IND: use channel(%d) sapi(%d) tei(%d) for now\n",
Harald Welte1fa60c82009-02-09 18:13:26 +0000135 l2addr.channel, l2addr.sapi, l2addr.tei);
136 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
137 if (!link) {
138 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
139 free(msg);
140 return -EINVAL;
141 }
142 /* save the channel number in the driver private struct */
143 link->driver.misdn.channel = l2addr.channel;
144 break;
145 case MPH_ACTIVATE_IND:
146 ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, l2addr.tei, l2addr.sapi);
147 break;
148 case MPH_DEACTIVATE_IND:
149 ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, l2addr.tei, l2addr.sapi);
150 break;
151 case DL_DATA_IND:
Harald Welte1fa60c82009-02-09 18:13:26 +0000152 msg->l2h = msg->data + MISDN_HEADER_LEN;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000153 DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
Harald Welte1fa60c82009-02-09 18:13:26 +0000154 ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
155 break;
156 default:
157 break;
158 }
159 return ret;
160}
161
162static int handle_ts1_write(struct bsc_fd *bfd)
163{
164 struct e1inp_line *line = bfd->data;
165 unsigned int ts_nr = bfd->priv_nr;
166 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
167 struct e1inp_sign_link *sign_link;
168 struct sockaddr_mISDN sa;
169 struct msgb *msg;
170 struct mISDNhead *hh;
171 u_int8_t *l2_data;
172 int ret;
173
174 /* get the next msg for this timeslot */
175 msg = e1inp_tx_ts(e1i_ts, &sign_link);
176 if (!msg) {
177 bfd->when &= ~BSC_FD_WRITE;
178 return 0;
179 }
180
181 l2_data = msg->data;
182
183 /* prepend the mISDNhead */
184 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
185 hh->prim = DL_DATA_REQ;
186
Holger Freytheracb688b2009-05-01 04:59:55 +0000187 DEBUGP(DMI, "TX TEI(%d) SAPI(%d): %s\n", sign_link->tei,
188 sign_link->sapi, hexdump(l2_data, msg->len - MISDN_HEADER_LEN));
Harald Welte1fa60c82009-02-09 18:13:26 +0000189
190 /* construct the sockaddr */
191 sa.family = AF_ISDN;
192 sa.sapi = sign_link->sapi;
193 sa.dev = sign_link->tei;
194 sa.channel = sign_link->driver.misdn.channel;
195
196 ret = sendto(bfd->fd, msg->data, msg->len, 0,
197 (struct sockaddr *)&sa, sizeof(sa));
Holger Freytheracb688b2009-05-01 04:59:55 +0000198 if (ret < 0)
199 fprintf(stderr, "%s sendto failed %d\n", __func__, ret);
Harald Welte1fa60c82009-02-09 18:13:26 +0000200 msgb_free(msg);
201
202 /* FIXME: this has to go */
203 usleep(100000);
204
205 return ret;
206}
207
Harald Weltef9227c72009-02-18 03:39:00 +0000208#define BCHAN_TX_GRAN 160
Harald Welte8ffcfed2009-02-10 18:18:50 +0000209/* write to a B channel TS */
210static int handle_tsX_write(struct bsc_fd *bfd)
211{
212 struct e1inp_line *line = bfd->data;
213 unsigned int ts_nr = bfd->priv_nr;
214 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
215 struct mISDNhead *hh;
216 u_int8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)];
217 struct subch_mux *mx = &e1i_ts->trau.mux;
218 int ret;
Harald Welte1fa60c82009-02-09 18:13:26 +0000219
Harald Welte8ffcfed2009-02-10 18:18:50 +0000220 hh = (struct mISDNhead *) tx_buf;
221 hh->prim = PH_DATA_REQ;
222
223 subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
224
Harald Welte3cc4bf52009-02-28 13:08:01 +0000225 DEBUGP(DMIB, "BCHAN TX: %s\n",
226 hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN));
Harald Welte8ffcfed2009-02-10 18:18:50 +0000227
228 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
229 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
230 DEBUGP(DMIB, "send returns %d instead of %u\n", ret,
231 sizeof(*hh) + BCHAN_TX_GRAN);
232
233 return ret;
234}
235
236#define TSX_ALLOC_SIZE 4096
Harald Welte1fa60c82009-02-09 18:13:26 +0000237/* FIXME: read from a B channel TS */
238static int handle_tsX_read(struct bsc_fd *bfd)
239{
240 struct e1inp_line *line = bfd->data;
241 unsigned int ts_nr = bfd->priv_nr;
242 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
243 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
244 struct mISDNhead *hh;
245 int ret;
246
247 if (!msg)
248 return -ENOMEM;
249
250 hh = (struct mISDNhead *) msg->data;
251
252 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
253 if (ret < 0) {
254 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
255 return ret;
256 }
257
258 msgb_put(msg, ret);
259
Holger Freytheracb688b2009-05-01 04:59:55 +0000260 if (hh->prim != PH_CONTROL_IND)
261 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
262 ret, hh->prim, hh->id, get_prim_name(hh->prim));
Harald Welte1fa60c82009-02-09 18:13:26 +0000263
264 switch (hh->prim) {
265 case PH_DATA_IND:
266 msg->l2h = msg->data + MISDN_HEADER_LEN;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000267 DEBUGP(DMIB, "BCHAN RX: %s\n",
268 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
Harald Welte1fa60c82009-02-09 18:13:26 +0000269 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
270 break;
Holger Freyther5d7e5572009-02-10 18:40:45 +0000271 case PH_ACTIVATE_IND:
Harald Welte8ffcfed2009-02-10 18:18:50 +0000272 case PH_DATA_CNF:
273 /* physical layer indicates that data has been sent,
274 * we thus can send some more data */
275 ret = handle_tsX_write(bfd);
Harald Welte1fa60c82009-02-09 18:13:26 +0000276 default:
277 break;
278 }
279 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
280 msgb_free(msg);
281
282 return ret;
283}
284
Harald Welte1fa60c82009-02-09 18:13:26 +0000285/* callback from select.c in case one of the fd's can be read/written */
286static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
287{
288 struct e1inp_line *line = bfd->data;
289 unsigned int ts_nr = bfd->priv_nr;
290 unsigned int idx = ts_nr-1;
291 struct e1inp_ts *e1i_ts = &line->ts[idx];
292 int rc = 0;
293
294 switch (e1i_ts->type) {
295 case E1INP_TS_TYPE_SIGN:
296 if (what & BSC_FD_READ)
297 rc = handle_ts1_read(bfd);
298 if (what & BSC_FD_WRITE)
299 rc = handle_ts1_write(bfd);
300 break;
301 case E1INP_TS_TYPE_TRAU:
302 if (what & BSC_FD_READ)
303 rc = handle_tsX_read(bfd);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000304 /* We never include the mISDN B-Channel FD into the
305 * writeset, since it doesn't support poll() based
306 * write flow control */
Harald Welte1fa60c82009-02-09 18:13:26 +0000307 break;
308 default:
309 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
310 break;
311 }
312
313 return rc;
314}
315
316static int activate_bchan(struct e1inp_line *line, int ts, int act)
317{
318 struct mISDNhead hh;
319 int ret;
320 unsigned int idx = ts-1;
321 struct e1inp_ts *e1i_ts = &line->ts[idx];
322 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
323
324 fprintf(stdout, "activate bchan\n");
325 if (act)
326 hh.prim = PH_ACTIVATE_REQ;
327 else
328 hh.prim = PH_DEACTIVATE_REQ;
329
330 hh.id = MISDN_ID_ANY;
331 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
332 if (ret < 0) {
333 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
334 strerror(errno));
335 }
336
337 return ret;
338}
339
340static int ts_want_write(struct e1inp_ts *e1i_ts)
341{
Harald Welte8ffcfed2009-02-10 18:18:50 +0000342 /* We never include the mISDN B-Channel FD into the
343 * writeset, since it doesn't support poll() based
344 * write flow control */
345 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
346 return 0;
347
Harald Welte1fa60c82009-02-09 18:13:26 +0000348 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
349
350 return 0;
351}
352
353struct e1inp_driver misdn_driver = {
354 .name = "mISDNuser",
355 .want_write = ts_want_write,
356};
357
Holger Freytherb5c00f52009-04-22 22:08:07 +0000358static int mi_e1_setup(struct e1inp_line *line, int release_l2)
Harald Welte1fa60c82009-02-09 18:13:26 +0000359{
360 struct mi_e1_handle *e1h = line->driver_data;
361 int ts, ret;
362
363 /* TS0 is CRC4, don't need any fd for it */
364 for (ts = 1; ts < NUM_E1_TS; ts++) {
365 unsigned int idx = ts-1;
366 struct e1inp_ts *e1i_ts = &line->ts[idx];
367 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
368 struct sockaddr_mISDN addr;
369
370 bfd->data = line;
371 bfd->priv_nr = ts;
372 bfd->cb = misdn_fd_cb;
373
374 switch (e1i_ts->type) {
375 case E1INP_TS_TYPE_NONE:
376 continue;
377 break;
378 case E1INP_TS_TYPE_SIGN:
379 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
380 bfd->when = BSC_FD_READ;
381 break;
382 case E1INP_TS_TYPE_TRAU:
383 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000384 /* We never include the mISDN B-Channel FD into the
385 * writeset, since it doesn't support poll() based
386 * write flow control */
387 bfd->when = BSC_FD_READ;
Harald Welte1fa60c82009-02-09 18:13:26 +0000388 break;
389 }
390
391 if (bfd->fd < 0) {
Holger Freytheracb688b2009-05-01 04:59:55 +0000392 fprintf(stderr, "%s could not open socket %s\n",
393 __func__, strerror(errno));
Harald Welte1fa60c82009-02-09 18:13:26 +0000394 return bfd->fd;
395 }
396
397 memset(&addr, 0, sizeof(addr));
398 addr.family = AF_ISDN;
399 addr.dev = e1h->cardnr;
400 switch (e1i_ts->type) {
401 case E1INP_TS_TYPE_SIGN:
402 addr.channel = 0;
403 /* SAPI not supported yet in kernel */
404 //addr.sapi = e1inp_ts->sign.sapi;
405 addr.sapi = 0;
406 addr.tei = GROUP_TEI;
407 break;
408 case E1INP_TS_TYPE_TRAU:
409 addr.channel = ts;
410 break;
411 default:
412 DEBUGP(DMI, "unsupported E1 TS type: %u\n",
413 e1i_ts->type);
414 break;
415 }
416
417 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
418 if (ret < 0) {
419 fprintf(stderr, "could not bind l2 socket %s\n",
420 strerror(errno));
421 return -EIO;
422 }
423
Holger Freytherb5c00f52009-04-22 22:08:07 +0000424 if (e1i_ts->type == E1INP_TS_TYPE_SIGN && release_l2) {
425 int clean = 1;
426 ret = ioctl(bfd->fd, IMCLEAR_L2, &clean);
427 if (ret < 0) {
428 fprintf(stderr, "could not send IOCTL IMCLEAN_L2 %s\n", strerror(errno));
429 return -EIO;
430 }
431 }
432
Harald Welte1fa60c82009-02-09 18:13:26 +0000433 /* FIXME: only activate B-Channels once we start to
434 * use them to conserve CPU power */
435 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
436 activate_bchan(line, ts, 1);
437
438 ret = bsc_register_fd(bfd);
439 if (ret < 0) {
440 fprintf(stderr, "could not register FD: %s\n",
441 strerror(ret));
442 return ret;
443 }
444 }
445
446 return 0;
447}
448
Holger Freytherb5c00f52009-04-22 22:08:07 +0000449int mi_setup(int cardnr, struct e1inp_line *line, int release_l2)
Harald Welte1fa60c82009-02-09 18:13:26 +0000450{
451 struct mi_e1_handle *e1h;
452 int sk, ret, cnt;
453 struct mISDN_devinfo devinfo;
454
455 /* register the driver with the core */
456 /* FIXME: do this in the plugin initializer function */
457 ret = e1inp_driver_register(&misdn_driver);
458 if (ret)
459 return ret;
460
461 /* create the actual line instance */
462 /* FIXME: do this independent of driver registration */
463 e1h = malloc(sizeof(*e1h));
464 memset(e1h, 0, sizeof(*e1h));
465
466 e1h->cardnr = cardnr;
467
468 line->driver = &misdn_driver;
469 line->driver_data = e1h;
470
471 /* open the ISDN card device */
472 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
473 if (sk < 0) {
Holger Freytheracb688b2009-05-01 04:59:55 +0000474 fprintf(stderr, "%s could not open socket %s\n",
475 __func__, strerror(errno));
Harald Welte1fa60c82009-02-09 18:13:26 +0000476 return sk;
477 }
478
479 ret = ioctl(sk, IMGETCOUNT, &cnt);
480 if (ret) {
Holger Freytheracb688b2009-05-01 04:59:55 +0000481 fprintf(stderr, "%s error getting interf count: %s\n",
482 __func__, strerror(errno));
Harald Welte1fa60c82009-02-09 18:13:26 +0000483 close(sk);
484 return -ENODEV;
485 }
486 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
487 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
488#if 1
489 devinfo.id = e1h->cardnr;
490 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
491 if (ret < 0) {
492 fprintf(stdout, "error getting info for device %d: %s\n",
493 e1h->cardnr, strerror(errno));
494 return -ENODEV;
495 }
496 fprintf(stdout, " id: %d\n", devinfo.id);
497 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
498 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
499 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
500 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
501 fprintf(stdout, " name: %s\n", devinfo.name);
502#endif
503
Holger Freytheracb688b2009-05-01 04:59:55 +0000504 if (!(devinfo.Dprotocols & (1 << ISDN_P_NT_E1))) {
505 fprintf(stderr, "error: card is not of type E1 (NT-mode)\n");
506 return -EINVAL;
507 }
508
509 if (devinfo.nrbchan != 30) {
510 fprintf(stderr, "error: E1 card has no 30 B-channels\n");
511 return -EINVAL;
512 }
513
Holger Freytherb5c00f52009-04-22 22:08:07 +0000514 ret = mi_e1_setup(line, release_l2);
Harald Welte1fa60c82009-02-09 18:13:26 +0000515 if (ret)
516 return ret;
517
518 return e1inp_line_register(line);
519}