blob: 8acf4c901425537e1621811dd72a2b513fa664de [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;
151 if (debug_mask & DMI) {
152 fprintf(stdout, "RX: ");
153 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
154 }
155 ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
156 break;
157 default:
158 break;
159 }
160 return ret;
161}
162
163static int handle_ts1_write(struct bsc_fd *bfd)
164{
165 struct e1inp_line *line = bfd->data;
166 unsigned int ts_nr = bfd->priv_nr;
167 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
168 struct e1inp_sign_link *sign_link;
169 struct sockaddr_mISDN sa;
170 struct msgb *msg;
171 struct mISDNhead *hh;
172 u_int8_t *l2_data;
173 int ret;
174
175 /* get the next msg for this timeslot */
176 msg = e1inp_tx_ts(e1i_ts, &sign_link);
177 if (!msg) {
178 bfd->when &= ~BSC_FD_WRITE;
179 return 0;
180 }
181
182 l2_data = msg->data;
183
184 /* prepend the mISDNhead */
185 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
186 hh->prim = DL_DATA_REQ;
187
188 if (debug_mask & DMI) {
Holger Freyther57dd7bf2009-02-10 01:04:22 +0000189 fprintf(stdout, "TX TEI(%d): ", sign_link->tei);
Harald Welte1fa60c82009-02-09 18:13:26 +0000190 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
191 }
192
193 /* construct the sockaddr */
194 sa.family = AF_ISDN;
195 sa.sapi = sign_link->sapi;
196 sa.dev = sign_link->tei;
197 sa.channel = sign_link->driver.misdn.channel;
198
199 ret = sendto(bfd->fd, msg->data, msg->len, 0,
200 (struct sockaddr *)&sa, sizeof(sa));
201 msgb_free(msg);
202
203 /* FIXME: this has to go */
204 usleep(100000);
205
206 return ret;
207}
208
Harald Weltef9227c72009-02-18 03:39:00 +0000209#define BCHAN_TX_GRAN 160
Harald Welte8ffcfed2009-02-10 18:18:50 +0000210/* write to a B channel TS */
211static int handle_tsX_write(struct bsc_fd *bfd)
212{
213 struct e1inp_line *line = bfd->data;
214 unsigned int ts_nr = bfd->priv_nr;
215 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
216 struct mISDNhead *hh;
217 u_int8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)];
218 struct subch_mux *mx = &e1i_ts->trau.mux;
219 int ret;
Harald Welte1fa60c82009-02-09 18:13:26 +0000220
Harald Welte8ffcfed2009-02-10 18:18:50 +0000221 hh = (struct mISDNhead *) tx_buf;
222 hh->prim = PH_DATA_REQ;
223
224 subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
225
226 if (debug_mask & DMIB) {
227 fprintf(stdout, "BCHAN TX: ");
228 hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
229 }
230
231 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
232 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
233 DEBUGP(DMIB, "send returns %d instead of %u\n", ret,
234 sizeof(*hh) + BCHAN_TX_GRAN);
235
236 return ret;
237}
238
239#define TSX_ALLOC_SIZE 4096
Harald Welte1fa60c82009-02-09 18:13:26 +0000240/* FIXME: read from a B channel TS */
241static int handle_tsX_read(struct bsc_fd *bfd)
242{
243 struct e1inp_line *line = bfd->data;
244 unsigned int ts_nr = bfd->priv_nr;
245 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
246 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
247 struct mISDNhead *hh;
248 int ret;
249
250 if (!msg)
251 return -ENOMEM;
252
253 hh = (struct mISDNhead *) msg->data;
254
255 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
256 if (ret < 0) {
257 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
258 return ret;
259 }
260
261 msgb_put(msg, ret);
262
263 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
264 ret, hh->prim, hh->id, get_prim_name(hh->prim));
265
266 switch (hh->prim) {
267 case PH_DATA_IND:
268 msg->l2h = msg->data + MISDN_HEADER_LEN;
269 if (debug_mask & DMIB) {
270 fprintf(stdout, "BCHAN RX: ");
271 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
272 }
273 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
274 break;
Holger Freyther5d7e5572009-02-10 18:40:45 +0000275 case PH_ACTIVATE_IND:
Harald Welte8ffcfed2009-02-10 18:18:50 +0000276 case PH_DATA_CNF:
277 /* physical layer indicates that data has been sent,
278 * we thus can send some more data */
279 ret = handle_tsX_write(bfd);
Harald Welte1fa60c82009-02-09 18:13:26 +0000280 default:
281 break;
282 }
283 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
284 msgb_free(msg);
285
286 return ret;
287}
288
Harald Welte1fa60c82009-02-09 18:13:26 +0000289/* callback from select.c in case one of the fd's can be read/written */
290static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
291{
292 struct e1inp_line *line = bfd->data;
293 unsigned int ts_nr = bfd->priv_nr;
294 unsigned int idx = ts_nr-1;
295 struct e1inp_ts *e1i_ts = &line->ts[idx];
296 int rc = 0;
297
298 switch (e1i_ts->type) {
299 case E1INP_TS_TYPE_SIGN:
300 if (what & BSC_FD_READ)
301 rc = handle_ts1_read(bfd);
302 if (what & BSC_FD_WRITE)
303 rc = handle_ts1_write(bfd);
304 break;
305 case E1INP_TS_TYPE_TRAU:
306 if (what & BSC_FD_READ)
307 rc = handle_tsX_read(bfd);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000308 /* We never include the mISDN B-Channel FD into the
309 * writeset, since it doesn't support poll() based
310 * write flow control */
Harald Welte1fa60c82009-02-09 18:13:26 +0000311 break;
312 default:
313 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
314 break;
315 }
316
317 return rc;
318}
319
320static int activate_bchan(struct e1inp_line *line, int ts, int act)
321{
322 struct mISDNhead hh;
323 int ret;
324 unsigned int idx = ts-1;
325 struct e1inp_ts *e1i_ts = &line->ts[idx];
326 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
327
328 fprintf(stdout, "activate bchan\n");
329 if (act)
330 hh.prim = PH_ACTIVATE_REQ;
331 else
332 hh.prim = PH_DEACTIVATE_REQ;
333
334 hh.id = MISDN_ID_ANY;
335 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
336 if (ret < 0) {
337 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
338 strerror(errno));
339 }
340
341 return ret;
342}
343
344static int ts_want_write(struct e1inp_ts *e1i_ts)
345{
Harald Welte8ffcfed2009-02-10 18:18:50 +0000346 /* We never include the mISDN B-Channel FD into the
347 * writeset, since it doesn't support poll() based
348 * write flow control */
349 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
350 return 0;
351
Harald Welte1fa60c82009-02-09 18:13:26 +0000352 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
353
354 return 0;
355}
356
357struct e1inp_driver misdn_driver = {
358 .name = "mISDNuser",
359 .want_write = ts_want_write,
360};
361
362static int mi_e1_setup(struct e1inp_line *line)
363{
364 struct mi_e1_handle *e1h = line->driver_data;
365 int ts, ret;
366
367 /* TS0 is CRC4, don't need any fd for it */
368 for (ts = 1; ts < NUM_E1_TS; ts++) {
369 unsigned int idx = ts-1;
370 struct e1inp_ts *e1i_ts = &line->ts[idx];
371 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
372 struct sockaddr_mISDN addr;
373
374 bfd->data = line;
375 bfd->priv_nr = ts;
376 bfd->cb = misdn_fd_cb;
377
378 switch (e1i_ts->type) {
379 case E1INP_TS_TYPE_NONE:
380 continue;
381 break;
382 case E1INP_TS_TYPE_SIGN:
383 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
384 bfd->when = BSC_FD_READ;
385 break;
386 case E1INP_TS_TYPE_TRAU:
387 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000388 /* We never include the mISDN B-Channel FD into the
389 * writeset, since it doesn't support poll() based
390 * write flow control */
391 bfd->when = BSC_FD_READ;
Harald Welte1fa60c82009-02-09 18:13:26 +0000392 break;
393 }
394
395 if (bfd->fd < 0) {
396 fprintf(stderr, "could not open socket %s\n",
397 strerror(errno));
398 return bfd->fd;
399 }
400
401 memset(&addr, 0, sizeof(addr));
402 addr.family = AF_ISDN;
403 addr.dev = e1h->cardnr;
404 switch (e1i_ts->type) {
405 case E1INP_TS_TYPE_SIGN:
406 addr.channel = 0;
407 /* SAPI not supported yet in kernel */
408 //addr.sapi = e1inp_ts->sign.sapi;
409 addr.sapi = 0;
410 addr.tei = GROUP_TEI;
411 break;
412 case E1INP_TS_TYPE_TRAU:
413 addr.channel = ts;
414 break;
415 default:
416 DEBUGP(DMI, "unsupported E1 TS type: %u\n",
417 e1i_ts->type);
418 break;
419 }
420
421 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
422 if (ret < 0) {
423 fprintf(stderr, "could not bind l2 socket %s\n",
424 strerror(errno));
425 return -EIO;
426 }
427
428 /* FIXME: only activate B-Channels once we start to
429 * use them to conserve CPU power */
430 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
431 activate_bchan(line, ts, 1);
432
433 ret = bsc_register_fd(bfd);
434 if (ret < 0) {
435 fprintf(stderr, "could not register FD: %s\n",
436 strerror(ret));
437 return ret;
438 }
439 }
440
441 return 0;
442}
443
444int mi_setup(int cardnr, struct e1inp_line *line)
445{
446 struct mi_e1_handle *e1h;
447 int sk, ret, cnt;
448 struct mISDN_devinfo devinfo;
449
450 /* register the driver with the core */
451 /* FIXME: do this in the plugin initializer function */
452 ret = e1inp_driver_register(&misdn_driver);
453 if (ret)
454 return ret;
455
456 /* create the actual line instance */
457 /* FIXME: do this independent of driver registration */
458 e1h = malloc(sizeof(*e1h));
459 memset(e1h, 0, sizeof(*e1h));
460
461 e1h->cardnr = cardnr;
462
463 line->driver = &misdn_driver;
464 line->driver_data = e1h;
465
466 /* open the ISDN card device */
467 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
468 if (sk < 0) {
469 fprintf(stderr, "could not open socket %s\n", strerror(errno));
470 return sk;
471 }
472
473 ret = ioctl(sk, IMGETCOUNT, &cnt);
474 if (ret) {
475 fprintf(stderr, "error getting interf count: %s\n",
476 strerror(errno));
477 close(sk);
478 return -ENODEV;
479 }
480 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
481 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
482#if 1
483 devinfo.id = e1h->cardnr;
484 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
485 if (ret < 0) {
486 fprintf(stdout, "error getting info for device %d: %s\n",
487 e1h->cardnr, strerror(errno));
488 return -ENODEV;
489 }
490 fprintf(stdout, " id: %d\n", devinfo.id);
491 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
492 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
493 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
494 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
495 fprintf(stdout, " name: %s\n", devinfo.name);
496#endif
497
498 ret = mi_e1_setup(line);
499 if (ret)
500 return ret;
501
502 return e1inp_line_register(line);
503}