blob: 9bf3aea0a676fbcf454318c571428f3ccb1d7645 [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>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020047#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020048#include <talloc.h>
Pablo Neira Ayuso0b099b22011-06-09 13:14:11 +020049#include <osmocom/abis/logging.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020050
51#define TS1_ALLOC_SIZE 300
52
53struct prim_name {
54 unsigned int prim;
55 const char *name;
56};
57
58const struct prim_name prim_names[] = {
59 { PH_CONTROL_IND, "PH_CONTROL_IND" },
60 { PH_DATA_IND, "PH_DATA_IND" },
61 { PH_DATA_CNF, "PH_DATA_CNF" },
62 { PH_ACTIVATE_IND, "PH_ACTIVATE_IND" },
63 { DL_ESTABLISH_IND, "DL_ESTABLISH_IND" },
64 { DL_ESTABLISH_CNF, "DL_ESTABLISH_CNF" },
65 { DL_RELEASE_IND, "DL_RELEASE_IND" },
66 { DL_RELEASE_CNF, "DL_RELEASE_CNF" },
67 { DL_DATA_IND, "DL_DATA_IND" },
68 { DL_UNITDATA_IND, "DL_UNITDATA_IND" },
69 { DL_INFORMATION_IND, "DL_INFORMATION_IND" },
70 { MPH_ACTIVATE_IND, "MPH_ACTIVATE_IND" },
71 { MPH_DEACTIVATE_IND, "MPH_DEACTIVATE_IND" },
72};
73
74const char *get_prim_name(unsigned int prim)
75{
76 int i;
77
78 for (i = 0; i < ARRAY_SIZE(prim_names); i++) {
79 if (prim_names[i].prim == prim)
80 return prim_names[i].name;
81 }
82
83 return "UNKNOWN";
84}
85
86static int handle_ts1_read(struct osmo_fd *bfd)
87{
88 struct e1inp_line *line = bfd->data;
89 unsigned int ts_nr = bfd->priv_nr;
90 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
91 struct e1inp_sign_link *link;
92 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "mISDN TS1");
93 struct sockaddr_mISDN l2addr;
94 struct mISDNhead *hh;
95 socklen_t alen;
96 int ret;
97
98 if (!msg)
99 return -ENOMEM;
100
101 hh = (struct mISDNhead *) msg->data;
102
103 alen = sizeof(l2addr);
104 ret = recvfrom(bfd->fd, msg->data, 300, 0,
105 (struct sockaddr *) &l2addr, &alen);
106 if (ret < 0) {
107 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
108 return ret;
109 }
110
111 if (alen != sizeof(l2addr)) {
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200112 fprintf(stderr, "%s error len\n", __func__);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200113 return -EINVAL;
114 }
115
116 msgb_put(msg, ret);
117
118 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
119 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
120
121 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x): %s\n",
122 ret, hh->prim, hh->id, get_prim_name(hh->prim));
123
124 switch (hh->prim) {
125 case DL_INFORMATION_IND:
126 /* mISDN tells us which channel number is allocated for this
127 * tuple of (SAPI, TEI). */
128 DEBUGP(DMI, "DL_INFORMATION_IND: use channel(%d) sapi(%d) tei(%d) for now\n",
129 l2addr.channel, l2addr.sapi, l2addr.tei);
130 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
131 if (!link) {
132 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
133 msgb_free(msg);
134 return -EINVAL;
135 }
136 /* save the channel number in the driver private struct */
137 link->driver.misdn.channel = l2addr.channel;
138 break;
139 case DL_ESTABLISH_IND:
140 DEBUGP(DMI, "DL_ESTABLISH_IND: channel(%d) sapi(%d) tei(%d)\n",
141 l2addr.channel, l2addr.sapi, l2addr.tei);
142 /* For some strange reason, sometimes the DL_INFORMATION_IND tells
143 * us the wrong channel, and we only get the real channel number
144 * during the DL_ESTABLISH_IND */
145 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
146 if (!link) {
147 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
148 msgb_free(msg);
149 return -EINVAL;
150 }
151 /* save the channel number in the driver private struct */
152 link->driver.misdn.channel = l2addr.channel;
153 ret = e1inp_event(e1i_ts, S_INP_TEI_UP, l2addr.tei, l2addr.sapi);
154 break;
155 case DL_RELEASE_IND:
156 DEBUGP(DMI, "DL_RELEASE_IND: channel(%d) sapi(%d) tei(%d)\n",
157 l2addr.channel, l2addr.sapi, l2addr.tei);
158 ret = e1inp_event(e1i_ts, S_INP_TEI_DN, l2addr.tei, l2addr.sapi);
159 break;
160 case DL_DATA_IND:
161 case DL_UNITDATA_IND:
162 msg->l2h = msg->data + MISDN_HEADER_LEN;
163 DEBUGP(DMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
164 ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
165 break;
166 case PH_ACTIVATE_IND:
167 DEBUGP(DMI, "PH_ACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
168 l2addr.channel, l2addr.sapi, l2addr.tei);
169 break;
170 case PH_DEACTIVATE_IND:
171 DEBUGP(DMI, "PH_DEACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
172 l2addr.channel, l2addr.sapi, l2addr.tei);
173 break;
174 default:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200175 break;
176 }
177 return ret;
178}
179
180static int ts_want_write(struct e1inp_ts *e1i_ts)
181{
182 /* We never include the mISDN B-Channel FD into the
183 * writeset, since it doesn't support poll() based
184 * write flow control */
185 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
186 return 0;
187
188 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
189
190 return 0;
191}
192
193static void timeout_ts1_write(void *data)
194{
195 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
196
197 /* trigger write of ts1, due to tx delay timer */
198 ts_want_write(e1i_ts);
199}
200
201static int handle_ts1_write(struct osmo_fd *bfd)
202{
203 struct e1inp_line *line = bfd->data;
204 unsigned int ts_nr = bfd->priv_nr;
205 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
206 struct e1inp_sign_link *sign_link;
207 struct sockaddr_mISDN sa;
208 struct msgb *msg;
209 struct mISDNhead *hh;
210 uint8_t *l2_data;
211 int ret;
212
213 bfd->when &= ~BSC_FD_WRITE;
214
215 /* get the next msg for this timeslot */
216 msg = e1inp_tx_ts(e1i_ts, &sign_link);
217 if (!msg) {
218 /* no message after tx delay timer */
219 return 0;
220 }
221
222 l2_data = msg->data;
223
224 /* prepend the mISDNhead */
225 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
226 hh->prim = DL_DATA_REQ;
227
228 DEBUGP(DMI, "TX channel(%d) TEI(%d) SAPI(%d): %s\n",
229 sign_link->driver.misdn.channel, sign_link->tei,
230 sign_link->sapi, osmo_hexdump(l2_data, msg->len - MISDN_HEADER_LEN));
231
232 /* construct the sockaddr */
233 sa.family = AF_ISDN;
234 sa.sapi = sign_link->sapi;
235 sa.dev = sign_link->tei;
236 sa.channel = sign_link->driver.misdn.channel;
237
238 ret = sendto(bfd->fd, msg->data, msg->len, 0,
239 (struct sockaddr *)&sa, sizeof(sa));
240 if (ret < 0)
241 fprintf(stderr, "%s sendto failed %d\n", __func__, ret);
242 msgb_free(msg);
243
244 /* set tx delay timer for next event */
245 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
246 e1i_ts->sign.tx_timer.data = e1i_ts;
247 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
248
249 return ret;
250}
251
252#define BCHAN_TX_GRAN 160
253/* write to a B channel TS */
254static int handle_tsX_write(struct osmo_fd *bfd)
255{
256 struct e1inp_line *line = bfd->data;
257 unsigned int ts_nr = bfd->priv_nr;
258 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
259 struct mISDNhead *hh;
260 uint8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)];
261 struct subch_mux *mx = &e1i_ts->trau.mux;
262 int ret;
263
264 hh = (struct mISDNhead *) tx_buf;
265 hh->prim = PH_DATA_REQ;
266
267 subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
268
269 DEBUGP(DMIB, "BCHAN TX: %s\n",
270 osmo_hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN));
271
272 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
273 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
274 DEBUGP(DMIB, "send returns %d instead of %zu\n", ret,
275 sizeof(*hh) + BCHAN_TX_GRAN);
276
277 return ret;
278}
279
280#define TSX_ALLOC_SIZE 4096
281/* FIXME: read from a B channel TS */
282static int handle_tsX_read(struct osmo_fd *bfd)
283{
284 struct e1inp_line *line = bfd->data;
285 unsigned int ts_nr = bfd->priv_nr;
286 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
287 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE, "mISDN TSx");
288 struct mISDNhead *hh;
289 int ret;
290
291 if (!msg)
292 return -ENOMEM;
293
294 hh = (struct mISDNhead *) msg->data;
295
296 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
297 if (ret < 0) {
298 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
299 return ret;
300 }
301
302 msgb_put(msg, ret);
303
304 if (hh->prim != PH_CONTROL_IND)
305 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
306 ret, hh->prim, hh->id, get_prim_name(hh->prim));
307
308 switch (hh->prim) {
309 case PH_DATA_IND:
310 msg->l2h = msg->data + MISDN_HEADER_LEN;
311 DEBUGP(DMIB, "BCHAN RX: %s\n",
312 osmo_hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
313 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
314 break;
315 case PH_ACTIVATE_IND:
316 case PH_DATA_CNF:
317 /* physical layer indicates that data has been sent,
318 * we thus can send some more data */
319 ret = handle_tsX_write(bfd);
320 default:
321 break;
322 }
323 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
324 msgb_free(msg);
325
326 return ret;
327}
328
329/* callback from select.c in case one of the fd's can be read/written */
330static int misdn_fd_cb(struct osmo_fd *bfd, unsigned int what)
331{
332 struct e1inp_line *line = bfd->data;
333 unsigned int ts_nr = bfd->priv_nr;
334 unsigned int idx = ts_nr-1;
335 struct e1inp_ts *e1i_ts = &line->ts[idx];
336 int rc = 0;
337
338 switch (e1i_ts->type) {
339 case E1INP_TS_TYPE_SIGN:
340 if (what & BSC_FD_READ)
341 rc = handle_ts1_read(bfd);
342 if (what & BSC_FD_WRITE)
343 rc = handle_ts1_write(bfd);
344 break;
345 case E1INP_TS_TYPE_TRAU:
346 if (what & BSC_FD_READ)
347 rc = handle_tsX_read(bfd);
348 /* We never include the mISDN B-Channel FD into the
349 * writeset, since it doesn't support poll() based
350 * write flow control */
351 break;
352 default:
353 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
354 break;
355 }
356
357 return rc;
358}
359
360static int activate_bchan(struct e1inp_line *line, int ts, int act)
361{
362 struct mISDNhead hh;
363 int ret;
364 unsigned int idx = ts-1;
365 struct e1inp_ts *e1i_ts = &line->ts[idx];
366 struct osmo_fd *bfd = &e1i_ts->driver.misdn.fd;
367
368 fprintf(stdout, "activate bchan\n");
369 if (act)
370 hh.prim = PH_ACTIVATE_REQ;
371 else
372 hh.prim = PH_DEACTIVATE_REQ;
373
374 hh.id = MISDN_ID_ANY;
375 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
376 if (ret < 0) {
377 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
378 strerror(errno));
379 }
380
381 return ret;
382}
383
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200384static int mi_e1_line_update(struct e1inp_line *line,
385 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200386
387struct e1inp_driver misdn_driver = {
388 .name = "misdn",
389 .want_write = ts_want_write,
390 .default_delay = 50000,
391 .line_update = &mi_e1_line_update,
392};
393
394static int mi_e1_setup(struct e1inp_line *line, int release_l2)
395{
396 int ts, ret;
397
398 /* TS0 is CRC4, don't need any fd for it */
399 for (ts = 1; ts < NUM_E1_TS; ts++) {
400 unsigned int idx = ts-1;
401 struct e1inp_ts *e1i_ts = &line->ts[idx];
402 struct osmo_fd *bfd = &e1i_ts->driver.misdn.fd;
403 struct sockaddr_mISDN addr;
404
405 bfd->data = line;
406 bfd->priv_nr = ts;
407 bfd->cb = misdn_fd_cb;
408
409 switch (e1i_ts->type) {
410 case E1INP_TS_TYPE_NONE:
411 continue;
412 break;
413 case E1INP_TS_TYPE_SIGN:
414 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
415 bfd->when = BSC_FD_READ;
416 break;
417 case E1INP_TS_TYPE_TRAU:
418 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
419 /* We never include the mISDN B-Channel FD into the
420 * writeset, since it doesn't support poll() based
421 * write flow control */
422 bfd->when = BSC_FD_READ;
423 break;
424 }
425
426 if (bfd->fd < 0) {
427 fprintf(stderr, "%s could not open socket %s\n",
428 __func__, strerror(errno));
429 return bfd->fd;
430 }
431
432 memset(&addr, 0, sizeof(addr));
433 addr.family = AF_ISDN;
434 addr.dev = line->num;
435 switch (e1i_ts->type) {
436 case E1INP_TS_TYPE_SIGN:
437 addr.channel = 0;
438 /* SAPI not supported yet in kernel */
439 //addr.sapi = e1inp_ts->sign.sapi;
440 addr.sapi = 0;
441 addr.tei = GROUP_TEI;
442 break;
443 case E1INP_TS_TYPE_TRAU:
444 addr.channel = ts;
445 break;
446 default:
447 DEBUGP(DMI, "unsupported E1 TS type: %u\n",
448 e1i_ts->type);
449 break;
450 }
451
452 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
453 if (ret < 0) {
454 fprintf(stderr, "could not bind l2 socket %s\n",
455 strerror(errno));
456 return -EIO;
457 }
458
459 if (e1i_ts->type == E1INP_TS_TYPE_SIGN) {
460 ret = ioctl(bfd->fd, IMCLEAR_L2, &release_l2);
461 if (ret < 0) {
462 fprintf(stderr, "could not send IOCTL IMCLEAN_L2 %s\n", strerror(errno));
463 return -EIO;
464 }
465 }
466
467 /* FIXME: only activate B-Channels once we start to
468 * use them to conserve CPU power */
469 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
470 activate_bchan(line, ts, 1);
471
472 ret = osmo_fd_register(bfd);
473 if (ret < 0) {
474 fprintf(stderr, "could not register FD: %s\n",
475 strerror(ret));
476 return ret;
477 }
478 }
479
480 return 0;
481}
482
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200483static int mi_e1_line_update(struct e1inp_line *line,
484 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485{
486 struct mISDN_devinfo devinfo;
487 int sk, ret, cnt;
488
489 if (line->driver != &misdn_driver)
490 return -EINVAL;
491
492 /* open the ISDN card device */
493 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
494 if (sk < 0) {
495 fprintf(stderr, "%s could not open socket %s\n",
496 __func__, strerror(errno));
497 return sk;
498 }
499
500 ret = ioctl(sk, IMGETCOUNT, &cnt);
501 if (ret) {
502 fprintf(stderr, "%s error getting interf count: %s\n",
503 __func__, strerror(errno));
504 close(sk);
505 return -ENODEV;
506 }
507 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
508 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
509#if 1
510 devinfo.id = line->num;
511 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
512 if (ret < 0) {
513 fprintf(stdout, "error getting info for device %d: %s\n",
514 line->num, strerror(errno));
515 return -ENODEV;
516 }
517 fprintf(stdout, " id: %d\n", devinfo.id);
518 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
519 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
520 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
521 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
522 fprintf(stdout, " name: %s\n", devinfo.name);
523#endif
524
525 if (!(devinfo.Dprotocols & (1 << ISDN_P_NT_E1))) {
526 fprintf(stderr, "error: card is not of type E1 (NT-mode)\n");
527 return -EINVAL;
528 }
529
530 ret = mi_e1_setup(line, 1);
531 if (ret)
532 return ret;
533
534 return 0;
535}
536
537void e1inp_misdn_init(void)
538{
539 /* register the driver with the core */
540 e1inp_driver_register(&misdn_driver);
541}