blob: 45d5e064b958931d5c25d0dc2252ea0d3d8e23db [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* 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 Affero General Public License as published by
10 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include "internal.h"
24
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <time.h>
31#include <sys/fcntl.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#ifndef AF_ISDN
40#define AF_ISDN 34
41#define PF_ISDN AF_ISDN
42#endif
43
44#include <osmocom/core/select.h>
45#include <osmocom/core/msgb.h>
46#include <osmocom/core/logging.h>
47#include <osmocom/gsm/abis/e1_input.h>
48#include <talloc.h>
49/*#include <openbsc/debug.h>
50#include <openbsc/gsm_data.h>
51#include <openbsc/abis_nm.h>
52#include <openbsc/abis_rsl.h>
53#include <openbsc/subchan_demux.h>
54#include <openbsc/e1_input.h>
55#include <openbsc/signal.h> */
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[] = {
65 { PH_CONTROL_IND, "PH_CONTROL_IND" },
66 { PH_DATA_IND, "PH_DATA_IND" },
67 { PH_DATA_CNF, "PH_DATA_CNF" },
68 { PH_ACTIVATE_IND, "PH_ACTIVATE_IND" },
69 { 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 osmo_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, "mISDN TS1");
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 fprintf(stderr, "%s error len\n", __func__);
119 return -EINVAL;
120 }
121
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
127 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x): %s\n",
128 ret, hh->prim, hh->id, get_prim_name(hh->prim));
129
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). */
134 DEBUGP(DMI, "DL_INFORMATION_IND: use channel(%d) sapi(%d) tei(%d) for now\n",
135 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 msgb_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 DL_ESTABLISH_IND:
146 DEBUGP(DMI, "DL_ESTABLISH_IND: channel(%d) sapi(%d) tei(%d)\n",
147 l2addr.channel, l2addr.sapi, l2addr.tei);
148 /* For some strange reason, sometimes the DL_INFORMATION_IND tells
149 * us the wrong channel, and we only get the real channel number
150 * during the DL_ESTABLISH_IND */
151 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
152 if (!link) {
153 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
154 msgb_free(msg);
155 return -EINVAL;
156 }
157 /* save the channel number in the driver private struct */
158 link->driver.misdn.channel = l2addr.channel;
159 ret = e1inp_event(e1i_ts, S_INP_TEI_UP, l2addr.tei, l2addr.sapi);
160 break;
161 case DL_RELEASE_IND:
162 DEBUGP(DMI, "DL_RELEASE_IND: channel(%d) sapi(%d) tei(%d)\n",
163 l2addr.channel, l2addr.sapi, l2addr.tei);
164 ret = e1inp_event(e1i_ts, S_INP_TEI_DN, l2addr.tei, l2addr.sapi);
165 break;
166 case DL_DATA_IND:
167 case DL_UNITDATA_IND:
168 msg->l2h = msg->data + MISDN_HEADER_LEN;
169 DEBUGP(DMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
170 ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
171 break;
172 case PH_ACTIVATE_IND:
173 DEBUGP(DMI, "PH_ACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
174 l2addr.channel, l2addr.sapi, l2addr.tei);
175 break;
176 case PH_DEACTIVATE_IND:
177 DEBUGP(DMI, "PH_DEACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
178 l2addr.channel, l2addr.sapi, l2addr.tei);
179 break;
180 default:
181 break;
182 }
183 return ret;
184}
185
186static int ts_want_write(struct e1inp_ts *e1i_ts)
187{
188 /* We never include the mISDN B-Channel FD into the
189 * writeset, since it doesn't support poll() based
190 * write flow control */
191 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
192 return 0;
193
194 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
195
196 return 0;
197}
198
199static void timeout_ts1_write(void *data)
200{
201 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
202
203 /* trigger write of ts1, due to tx delay timer */
204 ts_want_write(e1i_ts);
205}
206
207static int handle_ts1_write(struct osmo_fd *bfd)
208{
209 struct e1inp_line *line = bfd->data;
210 unsigned int ts_nr = bfd->priv_nr;
211 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
212 struct e1inp_sign_link *sign_link;
213 struct sockaddr_mISDN sa;
214 struct msgb *msg;
215 struct mISDNhead *hh;
216 uint8_t *l2_data;
217 int ret;
218
219 bfd->when &= ~BSC_FD_WRITE;
220
221 /* get the next msg for this timeslot */
222 msg = e1inp_tx_ts(e1i_ts, &sign_link);
223 if (!msg) {
224 /* no message after tx delay timer */
225 return 0;
226 }
227
228 l2_data = msg->data;
229
230 /* prepend the mISDNhead */
231 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
232 hh->prim = DL_DATA_REQ;
233
234 DEBUGP(DMI, "TX channel(%d) TEI(%d) SAPI(%d): %s\n",
235 sign_link->driver.misdn.channel, sign_link->tei,
236 sign_link->sapi, osmo_hexdump(l2_data, msg->len - MISDN_HEADER_LEN));
237
238 /* construct the sockaddr */
239 sa.family = AF_ISDN;
240 sa.sapi = sign_link->sapi;
241 sa.dev = sign_link->tei;
242 sa.channel = sign_link->driver.misdn.channel;
243
244 ret = sendto(bfd->fd, msg->data, msg->len, 0,
245 (struct sockaddr *)&sa, sizeof(sa));
246 if (ret < 0)
247 fprintf(stderr, "%s sendto failed %d\n", __func__, ret);
248 msgb_free(msg);
249
250 /* set tx delay timer for next event */
251 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
252 e1i_ts->sign.tx_timer.data = e1i_ts;
253 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
254
255 return ret;
256}
257
258#define BCHAN_TX_GRAN 160
259/* write to a B channel TS */
260static int handle_tsX_write(struct osmo_fd *bfd)
261{
262 struct e1inp_line *line = bfd->data;
263 unsigned int ts_nr = bfd->priv_nr;
264 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
265 struct mISDNhead *hh;
266 uint8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)];
267 struct subch_mux *mx = &e1i_ts->trau.mux;
268 int ret;
269
270 hh = (struct mISDNhead *) tx_buf;
271 hh->prim = PH_DATA_REQ;
272
273 subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
274
275 DEBUGP(DMIB, "BCHAN TX: %s\n",
276 osmo_hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN));
277
278 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
279 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
280 DEBUGP(DMIB, "send returns %d instead of %zu\n", ret,
281 sizeof(*hh) + BCHAN_TX_GRAN);
282
283 return ret;
284}
285
286#define TSX_ALLOC_SIZE 4096
287/* FIXME: read from a B channel TS */
288static int handle_tsX_read(struct osmo_fd *bfd)
289{
290 struct e1inp_line *line = bfd->data;
291 unsigned int ts_nr = bfd->priv_nr;
292 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
293 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE, "mISDN TSx");
294 struct mISDNhead *hh;
295 int ret;
296
297 if (!msg)
298 return -ENOMEM;
299
300 hh = (struct mISDNhead *) msg->data;
301
302 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
303 if (ret < 0) {
304 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
305 return ret;
306 }
307
308 msgb_put(msg, ret);
309
310 if (hh->prim != PH_CONTROL_IND)
311 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
312 ret, hh->prim, hh->id, get_prim_name(hh->prim));
313
314 switch (hh->prim) {
315 case PH_DATA_IND:
316 msg->l2h = msg->data + MISDN_HEADER_LEN;
317 DEBUGP(DMIB, "BCHAN RX: %s\n",
318 osmo_hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
319 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
320 break;
321 case PH_ACTIVATE_IND:
322 case PH_DATA_CNF:
323 /* physical layer indicates that data has been sent,
324 * we thus can send some more data */
325 ret = handle_tsX_write(bfd);
326 default:
327 break;
328 }
329 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
330 msgb_free(msg);
331
332 return ret;
333}
334
335/* callback from select.c in case one of the fd's can be read/written */
336static int misdn_fd_cb(struct osmo_fd *bfd, unsigned int what)
337{
338 struct e1inp_line *line = bfd->data;
339 unsigned int ts_nr = bfd->priv_nr;
340 unsigned int idx = ts_nr-1;
341 struct e1inp_ts *e1i_ts = &line->ts[idx];
342 int rc = 0;
343
344 switch (e1i_ts->type) {
345 case E1INP_TS_TYPE_SIGN:
346 if (what & BSC_FD_READ)
347 rc = handle_ts1_read(bfd);
348 if (what & BSC_FD_WRITE)
349 rc = handle_ts1_write(bfd);
350 break;
351 case E1INP_TS_TYPE_TRAU:
352 if (what & BSC_FD_READ)
353 rc = handle_tsX_read(bfd);
354 /* We never include the mISDN B-Channel FD into the
355 * writeset, since it doesn't support poll() based
356 * write flow control */
357 break;
358 default:
359 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
360 break;
361 }
362
363 return rc;
364}
365
366static int activate_bchan(struct e1inp_line *line, int ts, int act)
367{
368 struct mISDNhead hh;
369 int ret;
370 unsigned int idx = ts-1;
371 struct e1inp_ts *e1i_ts = &line->ts[idx];
372 struct osmo_fd *bfd = &e1i_ts->driver.misdn.fd;
373
374 fprintf(stdout, "activate bchan\n");
375 if (act)
376 hh.prim = PH_ACTIVATE_REQ;
377 else
378 hh.prim = PH_DEACTIVATE_REQ;
379
380 hh.id = MISDN_ID_ANY;
381 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
382 if (ret < 0) {
383 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
384 strerror(errno));
385 }
386
387 return ret;
388}
389
390static int
391mi_e1_line_update(struct e1inp_line *line, enum e1inp_line_role role);
392
393struct e1inp_driver misdn_driver = {
394 .name = "misdn",
395 .want_write = ts_want_write,
396 .default_delay = 50000,
397 .line_update = &mi_e1_line_update,
398};
399
400static int mi_e1_setup(struct e1inp_line *line, int release_l2)
401{
402 int ts, ret;
403
404 /* TS0 is CRC4, don't need any fd for it */
405 for (ts = 1; ts < NUM_E1_TS; ts++) {
406 unsigned int idx = ts-1;
407 struct e1inp_ts *e1i_ts = &line->ts[idx];
408 struct osmo_fd *bfd = &e1i_ts->driver.misdn.fd;
409 struct sockaddr_mISDN addr;
410
411 bfd->data = line;
412 bfd->priv_nr = ts;
413 bfd->cb = misdn_fd_cb;
414
415 switch (e1i_ts->type) {
416 case E1INP_TS_TYPE_NONE:
417 continue;
418 break;
419 case E1INP_TS_TYPE_SIGN:
420 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
421 bfd->when = BSC_FD_READ;
422 break;
423 case E1INP_TS_TYPE_TRAU:
424 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
425 /* We never include the mISDN B-Channel FD into the
426 * writeset, since it doesn't support poll() based
427 * write flow control */
428 bfd->when = BSC_FD_READ;
429 break;
430 }
431
432 if (bfd->fd < 0) {
433 fprintf(stderr, "%s could not open socket %s\n",
434 __func__, strerror(errno));
435 return bfd->fd;
436 }
437
438 memset(&addr, 0, sizeof(addr));
439 addr.family = AF_ISDN;
440 addr.dev = line->num;
441 switch (e1i_ts->type) {
442 case E1INP_TS_TYPE_SIGN:
443 addr.channel = 0;
444 /* SAPI not supported yet in kernel */
445 //addr.sapi = e1inp_ts->sign.sapi;
446 addr.sapi = 0;
447 addr.tei = GROUP_TEI;
448 break;
449 case E1INP_TS_TYPE_TRAU:
450 addr.channel = ts;
451 break;
452 default:
453 DEBUGP(DMI, "unsupported E1 TS type: %u\n",
454 e1i_ts->type);
455 break;
456 }
457
458 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
459 if (ret < 0) {
460 fprintf(stderr, "could not bind l2 socket %s\n",
461 strerror(errno));
462 return -EIO;
463 }
464
465 if (e1i_ts->type == E1INP_TS_TYPE_SIGN) {
466 ret = ioctl(bfd->fd, IMCLEAR_L2, &release_l2);
467 if (ret < 0) {
468 fprintf(stderr, "could not send IOCTL IMCLEAN_L2 %s\n", strerror(errno));
469 return -EIO;
470 }
471 }
472
473 /* FIXME: only activate B-Channels once we start to
474 * use them to conserve CPU power */
475 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
476 activate_bchan(line, ts, 1);
477
478 ret = osmo_fd_register(bfd);
479 if (ret < 0) {
480 fprintf(stderr, "could not register FD: %s\n",
481 strerror(ret));
482 return ret;
483 }
484 }
485
486 return 0;
487}
488
489static int
490mi_e1_line_update(struct e1inp_line *line, enum e1inp_line_role role)
491{
492 struct mISDN_devinfo devinfo;
493 int sk, ret, cnt;
494
495 if (line->driver != &misdn_driver)
496 return -EINVAL;
497
498 /* open the ISDN card device */
499 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
500 if (sk < 0) {
501 fprintf(stderr, "%s could not open socket %s\n",
502 __func__, strerror(errno));
503 return sk;
504 }
505
506 ret = ioctl(sk, IMGETCOUNT, &cnt);
507 if (ret) {
508 fprintf(stderr, "%s error getting interf count: %s\n",
509 __func__, strerror(errno));
510 close(sk);
511 return -ENODEV;
512 }
513 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
514 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
515#if 1
516 devinfo.id = line->num;
517 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
518 if (ret < 0) {
519 fprintf(stdout, "error getting info for device %d: %s\n",
520 line->num, strerror(errno));
521 return -ENODEV;
522 }
523 fprintf(stdout, " id: %d\n", devinfo.id);
524 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
525 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
526 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
527 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
528 fprintf(stdout, " name: %s\n", devinfo.name);
529#endif
530
531 if (!(devinfo.Dprotocols & (1 << ISDN_P_NT_E1))) {
532 fprintf(stderr, "error: card is not of type E1 (NT-mode)\n");
533 return -EINVAL;
534 }
535
536 ret = mi_e1_setup(line, 1);
537 if (ret)
538 return ret;
539
540 return 0;
541}
542
543void e1inp_misdn_init(void)
544{
545 /* register the driver with the core */
546 e1inp_driver_register(&misdn_driver);
547}