blob: e580730f18f7f01d23c9a8a884185d83e7ce4921 [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
208#define TSX_ALLOC_SIZE 4096
209
210/* FIXME: read from a B channel TS */
211static int handle_tsX_read(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 msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
217 struct mISDNhead *hh;
218 int ret;
219
220 if (!msg)
221 return -ENOMEM;
222
223 hh = (struct mISDNhead *) msg->data;
224
225 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
226 if (ret < 0) {
227 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
228 return ret;
229 }
230
231 msgb_put(msg, ret);
232
233 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
234 ret, hh->prim, hh->id, get_prim_name(hh->prim));
235
236 switch (hh->prim) {
237 case PH_DATA_IND:
238 msg->l2h = msg->data + MISDN_HEADER_LEN;
239 if (debug_mask & DMIB) {
240 fprintf(stdout, "BCHAN RX: ");
241 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
242 }
243 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
244 break;
245 default:
246 break;
247 }
248 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
249 msgb_free(msg);
250
251 return ret;
252}
253
254#define BCHAN_TX_GRAN 40
255/* write to a B channel TS */
256static int handle_tsX_write(struct bsc_fd *bfd)
257{
258 struct e1inp_line *line = bfd->data;
259 unsigned int ts_nr = bfd->priv_nr;
260 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
261 struct mISDNhead *hh;
262 u_int8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)];
263 struct subch_mux *mx = &e1i_ts->trau.mux;
264 int ret;
265
266 hh = (struct mISDNhead *) tx_buf;
267 hh->prim = PH_DATA_REQ;
268
269 subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
270
Holger Freyther57dd7bf2009-02-10 01:04:22 +0000271 if (debug_mask & DMIB) {
272 fprintf(stdout, "BCHAN TX: ");
273 hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
274 }
275
Harald Welte1fa60c82009-02-09 18:13:26 +0000276 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
277 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
278 DEBUGP(DMIB, "send returns %d instead of %u\n", ret,
279 sizeof(*hh) + BCHAN_TX_GRAN);
280
281 return ret;
282}
283
284/* callback from select.c in case one of the fd's can be read/written */
285static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
286{
287 struct e1inp_line *line = bfd->data;
288 unsigned int ts_nr = bfd->priv_nr;
289 unsigned int idx = ts_nr-1;
290 struct e1inp_ts *e1i_ts = &line->ts[idx];
291 int rc = 0;
292
293 switch (e1i_ts->type) {
294 case E1INP_TS_TYPE_SIGN:
295 if (what & BSC_FD_READ)
296 rc = handle_ts1_read(bfd);
297 if (what & BSC_FD_WRITE)
298 rc = handle_ts1_write(bfd);
299 break;
300 case E1INP_TS_TYPE_TRAU:
301 if (what & BSC_FD_READ)
302 rc = handle_tsX_read(bfd);
303 if (what & BSC_FD_WRITE)
304 rc = handle_tsX_write(bfd);
305 break;
306 default:
307 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
308 break;
309 }
310
311 return rc;
312}
313
314static int activate_bchan(struct e1inp_line *line, int ts, int act)
315{
316 struct mISDNhead hh;
317 int ret;
318 unsigned int idx = ts-1;
319 struct e1inp_ts *e1i_ts = &line->ts[idx];
320 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
321
322 fprintf(stdout, "activate bchan\n");
323 if (act)
324 hh.prim = PH_ACTIVATE_REQ;
325 else
326 hh.prim = PH_DEACTIVATE_REQ;
327
328 hh.id = MISDN_ID_ANY;
329 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
330 if (ret < 0) {
331 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
332 strerror(errno));
333 }
334
335 return ret;
336}
337
338static int ts_want_write(struct e1inp_ts *e1i_ts)
339{
340 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
341
342 return 0;
343}
344
345struct e1inp_driver misdn_driver = {
346 .name = "mISDNuser",
347 .want_write = ts_want_write,
348};
349
350static int mi_e1_setup(struct e1inp_line *line)
351{
352 struct mi_e1_handle *e1h = line->driver_data;
353 int ts, ret;
354
355 /* TS0 is CRC4, don't need any fd for it */
356 for (ts = 1; ts < NUM_E1_TS; ts++) {
357 unsigned int idx = ts-1;
358 struct e1inp_ts *e1i_ts = &line->ts[idx];
359 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
360 struct sockaddr_mISDN addr;
361
362 bfd->data = line;
363 bfd->priv_nr = ts;
364 bfd->cb = misdn_fd_cb;
365
366 switch (e1i_ts->type) {
367 case E1INP_TS_TYPE_NONE:
368 continue;
369 break;
370 case E1INP_TS_TYPE_SIGN:
371 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
372 bfd->when = BSC_FD_READ;
373 break;
374 case E1INP_TS_TYPE_TRAU:
375 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
376 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
377 break;
378 }
379
380 if (bfd->fd < 0) {
381 fprintf(stderr, "could not open socket %s\n",
382 strerror(errno));
383 return bfd->fd;
384 }
385
386 memset(&addr, 0, sizeof(addr));
387 addr.family = AF_ISDN;
388 addr.dev = e1h->cardnr;
389 switch (e1i_ts->type) {
390 case E1INP_TS_TYPE_SIGN:
391 addr.channel = 0;
392 /* SAPI not supported yet in kernel */
393 //addr.sapi = e1inp_ts->sign.sapi;
394 addr.sapi = 0;
395 addr.tei = GROUP_TEI;
396 break;
397 case E1INP_TS_TYPE_TRAU:
398 addr.channel = ts;
399 break;
400 default:
401 DEBUGP(DMI, "unsupported E1 TS type: %u\n",
402 e1i_ts->type);
403 break;
404 }
405
406 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
407 if (ret < 0) {
408 fprintf(stderr, "could not bind l2 socket %s\n",
409 strerror(errno));
410 return -EIO;
411 }
412
413 /* FIXME: only activate B-Channels once we start to
414 * use them to conserve CPU power */
415 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
416 activate_bchan(line, ts, 1);
417
418 ret = bsc_register_fd(bfd);
419 if (ret < 0) {
420 fprintf(stderr, "could not register FD: %s\n",
421 strerror(ret));
422 return ret;
423 }
424 }
425
426 return 0;
427}
428
429int mi_setup(int cardnr, struct e1inp_line *line)
430{
431 struct mi_e1_handle *e1h;
432 int sk, ret, cnt;
433 struct mISDN_devinfo devinfo;
434
435 /* register the driver with the core */
436 /* FIXME: do this in the plugin initializer function */
437 ret = e1inp_driver_register(&misdn_driver);
438 if (ret)
439 return ret;
440
441 /* create the actual line instance */
442 /* FIXME: do this independent of driver registration */
443 e1h = malloc(sizeof(*e1h));
444 memset(e1h, 0, sizeof(*e1h));
445
446 e1h->cardnr = cardnr;
447
448 line->driver = &misdn_driver;
449 line->driver_data = e1h;
450
451 /* open the ISDN card device */
452 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
453 if (sk < 0) {
454 fprintf(stderr, "could not open socket %s\n", strerror(errno));
455 return sk;
456 }
457
458 ret = ioctl(sk, IMGETCOUNT, &cnt);
459 if (ret) {
460 fprintf(stderr, "error getting interf count: %s\n",
461 strerror(errno));
462 close(sk);
463 return -ENODEV;
464 }
465 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
466 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
467#if 1
468 devinfo.id = e1h->cardnr;
469 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
470 if (ret < 0) {
471 fprintf(stdout, "error getting info for device %d: %s\n",
472 e1h->cardnr, strerror(errno));
473 return -ENODEV;
474 }
475 fprintf(stdout, " id: %d\n", devinfo.id);
476 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
477 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
478 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
479 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
480 fprintf(stdout, " name: %s\n", devinfo.name);
481#endif
482
483 ret = mi_e1_setup(line);
484 if (ret)
485 return ret;
486
487 return e1inp_line_register(line);
488}