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