blob: 7ee9930a04b8536251f6959bfeba3dad1564b3fe [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" },
Harald Welte1fa60c82009-02-09 18:13:26 +000068 { DL_ESTABLISH_IND, "DL_ESTABLISH_IND" },
69 { DL_ESTABLISH_CNF, "DL_ESTABLISH_CNF" },
70 { DL_RELEASE_IND, "DL_RELEASE_IND" },
71 { DL_RELEASE_CNF, "DL_RELEASE_CNF" },
72 { DL_DATA_IND, "DL_DATA_IND" },
73 { DL_UNITDATA_IND, "DL_UNITDATA_IND" },
74 { DL_INFORMATION_IND, "DL_INFORMATION_IND" },
75 { MPH_ACTIVATE_IND, "MPH_ACTIVATE_IND" },
76 { MPH_DEACTIVATE_IND, "MPH_DEACTIVATE_IND" },
77};
78
79const char *get_prim_name(unsigned int prim)
80{
81 int i;
82
83 for (i = 0; i < ARRAY_SIZE(prim_names); i++) {
84 if (prim_names[i].prim == prim)
85 return prim_names[i].name;
86 }
87
88 return "UNKNOWN";
89}
90
91static int handle_ts1_read(struct bsc_fd *bfd)
92{
93 struct e1inp_line *line = bfd->data;
94 unsigned int ts_nr = bfd->priv_nr;
95 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
96 struct e1inp_sign_link *link;
97 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
98 struct sockaddr_mISDN l2addr;
99 struct mISDNhead *hh;
100 socklen_t alen;
101 int ret;
102
103 if (!msg)
104 return -ENOMEM;
105
106 hh = (struct mISDNhead *) msg->data;
107
108 alen = sizeof(l2addr);
109 ret = recvfrom(bfd->fd, msg->data, 300, 0,
110 (struct sockaddr *) &l2addr, &alen);
111 if (ret < 0) {
112 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
113 return ret;
114 }
115
116 if (alen != sizeof(l2addr))
117 return -EINVAL;
118
119 msgb_put(msg, ret);
120
121 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
122 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
123
Holger Freyther57dd7bf2009-02-10 01:04:22 +0000124 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x): %s\n",
125 ret, hh->prim, hh->id, get_prim_name(hh->prim));
Harald Welte1fa60c82009-02-09 18:13:26 +0000126
127 switch (hh->prim) {
128 case DL_INFORMATION_IND:
129 /* mISDN tells us which channel number is allocated for this
130 * tuple of (SAPI, TEI). */
131 DEBUGP(DMI, "use channel(%d) sapi(%d) tei(%d) for now\n",
132 l2addr.channel, l2addr.sapi, l2addr.tei);
133 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
134 if (!link) {
135 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
136 free(msg);
137 return -EINVAL;
138 }
139 /* save the channel number in the driver private struct */
140 link->driver.misdn.channel = l2addr.channel;
141 break;
142 case MPH_ACTIVATE_IND:
143 ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, l2addr.tei, l2addr.sapi);
144 break;
145 case MPH_DEACTIVATE_IND:
146 ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, l2addr.tei, l2addr.sapi);
147 break;
148 case DL_DATA_IND:
Harald Welte1fa60c82009-02-09 18:13:26 +0000149 msg->l2h = msg->data + MISDN_HEADER_LEN;
150 if (debug_mask & DMI) {
151 fprintf(stdout, "RX: ");
152 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
153 }
154 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
187 if (debug_mask & DMI) {
Holger Freyther57dd7bf2009-02-10 01:04:22 +0000188 fprintf(stdout, "TX TEI(%d): ", sign_link->tei);
Harald Welte1fa60c82009-02-09 18:13:26 +0000189 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
190 }
191
192 /* construct the sockaddr */
193 sa.family = AF_ISDN;
194 sa.sapi = sign_link->sapi;
195 sa.dev = sign_link->tei;
196 sa.channel = sign_link->driver.misdn.channel;
197
198 ret = sendto(bfd->fd, msg->data, msg->len, 0,
199 (struct sockaddr *)&sa, sizeof(sa));
200 msgb_free(msg);
201
202 /* FIXME: this has to go */
203 usleep(100000);
204
205 return ret;
206}
207
Harald Welte8ffcfed2009-02-10 18:18:50 +0000208#define BCHAN_TX_GRAN 40
209/* 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
225 if (debug_mask & DMIB) {
226 fprintf(stdout, "BCHAN TX: ");
227 hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
228 }
229
230 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
231 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
232 DEBUGP(DMIB, "send returns %d instead of %u\n", ret,
233 sizeof(*hh) + BCHAN_TX_GRAN);
234
235 return ret;
236}
237
238#define TSX_ALLOC_SIZE 4096
Harald Welte1fa60c82009-02-09 18:13:26 +0000239/* FIXME: read from a B channel TS */
240static int handle_tsX_read(struct bsc_fd *bfd)
241{
242 struct e1inp_line *line = bfd->data;
243 unsigned int ts_nr = bfd->priv_nr;
244 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
245 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
246 struct mISDNhead *hh;
247 int ret;
248
249 if (!msg)
250 return -ENOMEM;
251
252 hh = (struct mISDNhead *) msg->data;
253
254 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
255 if (ret < 0) {
256 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
257 return ret;
258 }
259
260 msgb_put(msg, ret);
261
262 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
263 ret, hh->prim, hh->id, get_prim_name(hh->prim));
264
265 switch (hh->prim) {
266 case PH_DATA_IND:
267 msg->l2h = msg->data + MISDN_HEADER_LEN;
268 if (debug_mask & DMIB) {
269 fprintf(stdout, "BCHAN RX: ");
270 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
271 }
272 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
273 break;
Harald Welte8ffcfed2009-02-10 18:18:50 +0000274 case PH_DATA_CNF:
275 /* physical layer indicates that data has been sent,
276 * we thus can send some more data */
277 ret = handle_tsX_write(bfd);
Harald Welte1fa60c82009-02-09 18:13:26 +0000278 default:
279 break;
280 }
281 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
282 msgb_free(msg);
283
284 return ret;
285}
286
Harald Welte1fa60c82009-02-09 18:13:26 +0000287/* callback from select.c in case one of the fd's can be read/written */
288static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
289{
290 struct e1inp_line *line = bfd->data;
291 unsigned int ts_nr = bfd->priv_nr;
292 unsigned int idx = ts_nr-1;
293 struct e1inp_ts *e1i_ts = &line->ts[idx];
294 int rc = 0;
295
296 switch (e1i_ts->type) {
297 case E1INP_TS_TYPE_SIGN:
298 if (what & BSC_FD_READ)
299 rc = handle_ts1_read(bfd);
300 if (what & BSC_FD_WRITE)
301 rc = handle_ts1_write(bfd);
302 break;
303 case E1INP_TS_TYPE_TRAU:
304 if (what & BSC_FD_READ)
305 rc = handle_tsX_read(bfd);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000306 /* We never include the mISDN B-Channel FD into the
307 * writeset, since it doesn't support poll() based
308 * write flow control */
Harald Welte1fa60c82009-02-09 18:13:26 +0000309 break;
310 default:
311 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
312 break;
313 }
314
315 return rc;
316}
317
318static int activate_bchan(struct e1inp_line *line, int ts, int act)
319{
320 struct mISDNhead hh;
321 int ret;
322 unsigned int idx = ts-1;
323 struct e1inp_ts *e1i_ts = &line->ts[idx];
324 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
325
326 fprintf(stdout, "activate bchan\n");
327 if (act)
328 hh.prim = PH_ACTIVATE_REQ;
329 else
330 hh.prim = PH_DEACTIVATE_REQ;
331
332 hh.id = MISDN_ID_ANY;
333 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
334 if (ret < 0) {
335 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
336 strerror(errno));
337 }
338
339 return ret;
340}
341
342static int ts_want_write(struct e1inp_ts *e1i_ts)
343{
Harald Welte8ffcfed2009-02-10 18:18:50 +0000344 /* We never include the mISDN B-Channel FD into the
345 * writeset, since it doesn't support poll() based
346 * write flow control */
347 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
348 return 0;
349
Harald Welte1fa60c82009-02-09 18:13:26 +0000350 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
351
352 return 0;
353}
354
355struct e1inp_driver misdn_driver = {
356 .name = "mISDNuser",
357 .want_write = ts_want_write,
358};
359
360static int mi_e1_setup(struct e1inp_line *line)
361{
362 struct mi_e1_handle *e1h = line->driver_data;
363 int ts, ret;
364
365 /* TS0 is CRC4, don't need any fd for it */
366 for (ts = 1; ts < NUM_E1_TS; ts++) {
367 unsigned int idx = ts-1;
368 struct e1inp_ts *e1i_ts = &line->ts[idx];
369 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
370 struct sockaddr_mISDN addr;
371
372 bfd->data = line;
373 bfd->priv_nr = ts;
374 bfd->cb = misdn_fd_cb;
375
376 switch (e1i_ts->type) {
377 case E1INP_TS_TYPE_NONE:
378 continue;
379 break;
380 case E1INP_TS_TYPE_SIGN:
381 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
382 bfd->when = BSC_FD_READ;
383 break;
384 case E1INP_TS_TYPE_TRAU:
385 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
Harald Welte8ffcfed2009-02-10 18:18:50 +0000386 /* We never include the mISDN B-Channel FD into the
387 * writeset, since it doesn't support poll() based
388 * write flow control */
389 bfd->when = BSC_FD_READ;
Harald Welte1fa60c82009-02-09 18:13:26 +0000390 break;
391 }
392
393 if (bfd->fd < 0) {
394 fprintf(stderr, "could not open socket %s\n",
395 strerror(errno));
396 return bfd->fd;
397 }
398
399 memset(&addr, 0, sizeof(addr));
400 addr.family = AF_ISDN;
401 addr.dev = e1h->cardnr;
402 switch (e1i_ts->type) {
403 case E1INP_TS_TYPE_SIGN:
404 addr.channel = 0;
405 /* SAPI not supported yet in kernel */
406 //addr.sapi = e1inp_ts->sign.sapi;
407 addr.sapi = 0;
408 addr.tei = GROUP_TEI;
409 break;
410 case E1INP_TS_TYPE_TRAU:
411 addr.channel = ts;
412 break;
413 default:
414 DEBUGP(DMI, "unsupported E1 TS type: %u\n",
415 e1i_ts->type);
416 break;
417 }
418
419 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
420 if (ret < 0) {
421 fprintf(stderr, "could not bind l2 socket %s\n",
422 strerror(errno));
423 return -EIO;
424 }
425
426 /* FIXME: only activate B-Channels once we start to
427 * use them to conserve CPU power */
428 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
429 activate_bchan(line, ts, 1);
430
431 ret = bsc_register_fd(bfd);
432 if (ret < 0) {
433 fprintf(stderr, "could not register FD: %s\n",
434 strerror(ret));
435 return ret;
436 }
437 }
438
439 return 0;
440}
441
442int mi_setup(int cardnr, struct e1inp_line *line)
443{
444 struct mi_e1_handle *e1h;
445 int sk, ret, cnt;
446 struct mISDN_devinfo devinfo;
447
448 /* register the driver with the core */
449 /* FIXME: do this in the plugin initializer function */
450 ret = e1inp_driver_register(&misdn_driver);
451 if (ret)
452 return ret;
453
454 /* create the actual line instance */
455 /* FIXME: do this independent of driver registration */
456 e1h = malloc(sizeof(*e1h));
457 memset(e1h, 0, sizeof(*e1h));
458
459 e1h->cardnr = cardnr;
460
461 line->driver = &misdn_driver;
462 line->driver_data = e1h;
463
464 /* open the ISDN card device */
465 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
466 if (sk < 0) {
467 fprintf(stderr, "could not open socket %s\n", strerror(errno));
468 return sk;
469 }
470
471 ret = ioctl(sk, IMGETCOUNT, &cnt);
472 if (ret) {
473 fprintf(stderr, "error getting interf count: %s\n",
474 strerror(errno));
475 close(sk);
476 return -ENODEV;
477 }
478 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
479 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
480#if 1
481 devinfo.id = e1h->cardnr;
482 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
483 if (ret < 0) {
484 fprintf(stdout, "error getting info for device %d: %s\n",
485 e1h->cardnr, strerror(errno));
486 return -ENODEV;
487 }
488 fprintf(stdout, " id: %d\n", devinfo.id);
489 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
490 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
491 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
492 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
493 fprintf(stdout, " name: %s\n", devinfo.name);
494#endif
495
496 ret = mi_e1_setup(line);
497 if (ret)
498 return ret;
499
500 return e1inp_line_register(line);
501}