blob: 366580c221410203a26bbf64c67c66c63a281e40 [file] [log] [blame]
Harald Welte8470bf22008-12-25 23:28:35 +00001/* OpenBSC Abis interface to mISDNuser */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Harald Welte52b1f982008-12-23 20:25:15 +00004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdio.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <string.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <sys/ioctl.h>
31#include <mISDNif.h>
32
Harald Welte8470bf22008-12-25 23:28:35 +000033//#define AF_COMPATIBILITY_FUNC
34//#include <compat_af_isdn.h>
35#define AF_ISDN 34
36#define PF_ISDN AF_ISDN
37
38#include <openbsc/select.h>
39#include <openbsc/msgb.h>
40#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
42#include <openbsc/abis_nm.h>
43#include <openbsc/abis_rsl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000044
45#define NUM_E1_TS 32
46
47/* data structure for one E1 interface with A-bis */
48struct mi_e1_handle {
49 struct gsm_bts *bts;
Harald Welte52b1f982008-12-23 20:25:15 +000050 /* The mISDN card number of the card we use */
51 int cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +000052 /* The RSL adress */
53 struct sockaddr_mISDN l2addr;
Harald Welte52b1f982008-12-23 20:25:15 +000054 /* The OML adress */
55 struct sockaddr_mISDN omladdr;
Harald Welte8470bf22008-12-25 23:28:35 +000056 /* list (queue) of to-be-sent msgb's */
57 struct llist_head rsl_tx_list;
58 struct llist_head oml_tx_list;
Harald Welte52b1f982008-12-23 20:25:15 +000059
Harald Weltead384642008-12-26 10:20:07 +000060 void (*cb)(int event, struct gsm_bts *bts);
Harald Welte8470bf22008-12-25 23:28:35 +000061 struct bsc_fd fd[NUM_E1_TS];
Harald Welte52b1f982008-12-23 20:25:15 +000062};
63
Harald Welte8470bf22008-12-25 23:28:35 +000064/* FIXME: this needs to go */
65static struct mi_e1_handle *global_e1h;
66
Harald Welte52b1f982008-12-23 20:25:15 +000067#define SAPI_L2ML 0
68#define SAPI_OML 62
Harald Weltead384642008-12-26 10:20:07 +000069#define SAPI_RSL 0 /* 63 ? */
Harald Welte52b1f982008-12-23 20:25:15 +000070
71#define TEI_L2ML 127
72#define TEI_OML 25
73#define TEI_RSL 1
74
75static void hexdump(unsigned char *buf, int len)
76{
77 int i;
78 for (i = 0; i < len; i++) {
79 fprintf(stdout, "%02x ", buf[i]);
80 }
81 fprintf(stdout, "\n");
82}
83
84#define TS1_ALLOC_SIZE 300
85
86static int handle_ts1_read(struct bsc_fd *bfd)
87{
88 struct mi_e1_handle *e1h = bfd->data;
89 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
Harald Welte8470bf22008-12-25 23:28:35 +000090 struct sockaddr_mISDN l2addr;
91 struct mISDNhead *hh;
Harald Welte52b1f982008-12-23 20:25:15 +000092 socklen_t alen;
Harald Welte8470bf22008-12-25 23:28:35 +000093 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +000094
95 if (!msg)
96 return -ENOMEM;
97
Harald Welte8470bf22008-12-25 23:28:35 +000098 hh = (struct mISDNhead *) msg->data;
99
100 /* FIXME: Map TEI/SAPI to TRX */
101 msg->trx = e1h->bts->c0;
Harald Welte52b1f982008-12-23 20:25:15 +0000102
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))
112 return -EINVAL;
113
114 msgb_put(msg, ret);
115
116 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
117 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
118
119 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n",
120 ret, hh->prim, hh->id);
121
122 switch (hh->prim) {
123 case DL_INFORMATION_IND:
124 DEBUGP(DMI, "got DL_INFORMATION_IND\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000125 struct sockaddr_mISDN *sa = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000126 char *lstr = "UNKN";
127
128 switch (l2addr.tei) {
129 case TEI_OML:
130 sa = &e1h->omladdr;
131 lstr = "OML";
132 break;
133 case TEI_RSL:
134 sa = &e1h->l2addr;
135 lstr = "RSL";
136 break;
137 default:
Harald Welte8470bf22008-12-25 23:28:35 +0000138 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000139 }
Harald Welte8470bf22008-12-25 23:28:35 +0000140 if (sa) {
141 DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n",
142 lstr, l2addr.channel, l2addr.sapi, l2addr.tei);
143 memcpy(sa, &l2addr, sizeof(l2addr));
144 }
Harald Welte52b1f982008-12-23 20:25:15 +0000145 break;
146 case DL_ESTABLISH_IND:
147 DEBUGP(DMI, "got DL_ESTABLISH_IND\n");
148 break;
149 case DL_ESTABLISH_CNF:
150 DEBUGP(DMI, "got DL_ESTABLISH_CNF\n");
151 break;
152 case DL_RELEASE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000153 DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000154 break;
155 case MPH_ACTIVATE_IND:
156 DEBUGP(DMI, "got MPH_ACTIVATE_IND\n");
Harald Weltead384642008-12-26 10:20:07 +0000157 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
158 e1h->cb(EVT_E1_OML_UP, e1h->bts);
159 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
160 e1h->cb(EVT_E1_RSL_UP, e1h->bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000161 break;
162 case MPH_DEACTIVATE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000163 DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n");
164 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
165 e1h->cb(EVT_E1_OML_DN, e1h->bts);
166 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
167 e1h->cb(EVT_E1_RSL_DN, e1h->bts);
168 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000169 case DL_DATA_IND:
170 DEBUGP(DMI, "got DL_DATA_IND\n");
Harald Weltead384642008-12-26 10:20:07 +0000171
172 /* FIXME: this stinks */
173 msg->trx = e1h->bts->c0;
174
175 msg->l2h = msg->data + MISDN_HEADER_LEN;
176
177 fprintf(stdout, "RX: ");
Harald Welte52b1f982008-12-23 20:25:15 +0000178 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
179 switch (l2addr.tei) {
180 case TEI_OML:
181 ret = abis_nm_rcvmsg(msg);
182 break;
183 case TEI_RSL:
184 ret = abis_rsl_rcvmsg(msg);
185 break;
186 default:
187 fprintf(stderr, "DATA_IND for unknown TEI\n");
188 break;
189 }
190 break;
191 default:
192 DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim);
193 break;
194 }
195 return ret;
196}
197
198static int handle_ts1_write(struct bsc_fd *bfd)
199{
200 struct mi_e1_handle *e1h = bfd->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000201 struct msgb *msg;
202 struct mISDNhead *hh;
Harald Weltead384642008-12-26 10:20:07 +0000203 int ret, no_oml = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000204
Harald Weltead384642008-12-26 10:20:07 +0000205 msg = msgb_dequeue(&e1h->oml_tx_list);
Harald Welte8470bf22008-12-25 23:28:35 +0000206 if (!msg)
Harald Weltead384642008-12-26 10:20:07 +0000207 no_oml = 1;
Harald Welte8470bf22008-12-25 23:28:35 +0000208 else {
Harald Weltead384642008-12-26 10:20:07 +0000209 u_int8_t *l2_data = msg->data;
210
Harald Welte8470bf22008-12-25 23:28:35 +0000211 /* prepend the mISDNhead */
212 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
213 hh->prim = DL_DATA_REQ;
Harald Welte52b1f982008-12-23 20:25:15 +0000214
Harald Weltead384642008-12-26 10:20:07 +0000215 fprintf(stdout, "OML TX: ");
216 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
217
Harald Welte8470bf22008-12-25 23:28:35 +0000218 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000219 (struct sockaddr *)&e1h->omladdr,
220 sizeof(e1h->omladdr));
Harald Weltee571fb82008-12-25 23:50:30 +0000221 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000222 usleep(100000);
Harald Weltead384642008-12-26 10:20:07 +0000223 /* we always dequeue all OML messages */
224 return ret;
Harald Welte8470bf22008-12-25 23:28:35 +0000225 }
Harald Weltead384642008-12-26 10:20:07 +0000226
Harald Welte8470bf22008-12-25 23:28:35 +0000227 msg = msgb_dequeue(&e1h->rsl_tx_list);
228 if (!msg) {
Harald Weltead384642008-12-26 10:20:07 +0000229 if (no_oml)
Harald Welte8470bf22008-12-25 23:28:35 +0000230 bfd->when &= ~BSC_FD_WRITE;
231 } else {
Harald Weltead384642008-12-26 10:20:07 +0000232 u_int8_t *l2_data = msg->data;
233
Harald Welte8470bf22008-12-25 23:28:35 +0000234 /* prepend the mISDNhead */
235 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
236 hh->prim = DL_DATA_REQ;
237
Harald Weltead384642008-12-26 10:20:07 +0000238 fprintf(stdout, "RSL TX: ");
239 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
240
Harald Welte8470bf22008-12-25 23:28:35 +0000241 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000242 (struct sockaddr *)&e1h->l2addr,
243 sizeof(e1h->l2addr));
Harald Weltee571fb82008-12-25 23:50:30 +0000244 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000245 usleep(100000);
246 }
247
248 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000249}
250
251static int handle_tsX_read(struct bsc_fd *bfd)
252{
253 /* FIXME: read from a B channel TS */
254}
255
Harald Welte8470bf22008-12-25 23:28:35 +0000256static int handle_tsX_write(struct bsc_fd *bfd)
Harald Welte52b1f982008-12-23 20:25:15 +0000257{
258 /* FIXME: write to a B channel TS */
259}
260
261/* callback from select.c in case one of the fd's can be read/written */
Harald Welte8470bf22008-12-25 23:28:35 +0000262static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
Harald Welte52b1f982008-12-23 20:25:15 +0000263{
264 unsigned int e1_ts = bfd->priv_nr;
265 int rc = 0;
266
267 switch (e1_ts) {
268 case 1:
269 if (what & BSC_FD_READ)
270 rc = handle_ts1_read(bfd);
271 if (what & BSC_FD_WRITE)
272 rc = handle_ts1_write(bfd);
273 break;
274 default:
275 if (what & BSC_FD_READ)
276 rc = handle_tsX_read(bfd);
277 if (what & BSC_FD_WRITE)
278 rc = handle_tsX_write(bfd);
279 break;
280 }
281
282 return rc;
283}
284
Harald Welte8470bf22008-12-25 23:28:35 +0000285int abis_rsl_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000286{
Harald Welte8470bf22008-12-25 23:28:35 +0000287 struct mi_e1_handle *e1h = global_e1h;
288
Harald Weltead384642008-12-26 10:20:07 +0000289 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000290 msgb_enqueue(&e1h->rsl_tx_list, msg);
291 e1h->fd[0].when |= BSC_FD_WRITE;
292
293 return 0;
294}
295
Harald Weltead384642008-12-26 10:20:07 +0000296int _abis_nm_sendmsg(struct msgb *msg)
Harald Welte8470bf22008-12-25 23:28:35 +0000297{
298 struct mi_e1_handle *e1h = global_e1h;
299
Harald Weltead384642008-12-26 10:20:07 +0000300 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000301 msgb_enqueue(&e1h->oml_tx_list, msg);
302 e1h->fd[0].when |= BSC_FD_WRITE;
303
304 return 0;
305}
306
307static int mi_e1_setup(struct mi_e1_handle *e1h)
308{
309 int ts, sk, ret, cnt;
310 struct mISDN_devinfo devinfo;
Harald Welte52b1f982008-12-23 20:25:15 +0000311
312 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
Harald Welte8470bf22008-12-25 23:28:35 +0000313 if (sk < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000314 fprintf(stderr, "could not open socket %s\n", strerror(errno));
315 return sk;
316 }
317
318 ret = ioctl(sk, IMGETCOUNT, &cnt);
319 if (ret) {
320 fprintf(stderr, "error getting interf count: %s\n",
321 strerror(errno));
322 close(sk);
323 return -ENODEV;
324 }
Harald Weltead384642008-12-26 10:20:07 +0000325 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
326 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
327#if 1
328 devinfo.id = e1h->cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000329 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
330 if (ret < 0) {
331 fprintf(stdout, "error getting info for device %d: %s\n",
Harald Weltead384642008-12-26 10:20:07 +0000332 e1h->cardnr, strerror(errno));
Harald Welte52b1f982008-12-23 20:25:15 +0000333 return -ENODEV;
334 }
335 fprintf(stdout, " id: %d\n", devinfo.id);
336 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
337 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
338 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
339 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
340 fprintf(stdout, " name: %s\n", devinfo.name);
341#endif
342
343 /* TS0 is CRC4, don't need any fd for it */
344 for (ts = 1; ts < NUM_E1_TS; ts++) {
Harald Welte8470bf22008-12-25 23:28:35 +0000345 unsigned int idx = ts-1;
Harald Welte52b1f982008-12-23 20:25:15 +0000346 struct bsc_fd *bfd = &e1h->fd[idx];
347 struct sockaddr_mISDN addr;
348
Harald Welte8470bf22008-12-25 23:28:35 +0000349 bfd->data = e1h;
350 bfd->priv_nr = ts;
351 bfd->cb = misdn_fd_cb;
352
353 if (ts == 1) {
Harald Weltead384642008-12-26 10:20:07 +0000354 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
Harald Welte8470bf22008-12-25 23:28:35 +0000355 bfd->when = BSC_FD_READ;
356 } else
Harald Welte52b1f982008-12-23 20:25:15 +0000357 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
358
Harald Welte8470bf22008-12-25 23:28:35 +0000359
360 if (bfd->fd < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000361 fprintf(stderr, "could not open socket %s\n",
362 strerror(errno));
363 return bfd->fd;
364 }
365
366 memset(&addr, 0, sizeof(addr));
367 addr.family = AF_ISDN;
368 addr.dev = e1h->cardnr;
369 if (ts == 1) {
370 addr.channel = 0;
371 addr.sapi = 0;/* SAPI not supported yet in kernel */
372 addr.tei = TEI_L2ML;
373 } else
374 addr.channel = ts;
375
376 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
377 if (ret < 0) {
Harald Welte8470bf22008-12-25 23:28:35 +0000378 fprintf(stderr, "could not bind l2 socket %s\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000379 strerror(errno));
380 return -EIO;
381 }
Harald Welte8470bf22008-12-25 23:28:35 +0000382
383 ret = bsc_register_fd(bfd);
384 if (ret < 0) {
385 fprintf(stderr, "could not register FD: %s\n",
386 strerror(ret));
387 return ret;
388 }
Harald Welte52b1f982008-12-23 20:25:15 +0000389 }
Harald Welte8470bf22008-12-25 23:28:35 +0000390
391 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000392}
393
Harald Weltead384642008-12-26 10:20:07 +0000394int mi_setup(struct gsm_bts *bts, int cardnr,
395 void (cb)(int event, struct gsm_bts *bts))
Harald Welte8470bf22008-12-25 23:28:35 +0000396{
397 struct mi_e1_handle *e1h;
398
399 e1h = malloc(sizeof(*e1h));
400 memset(e1h, 0, sizeof(*e1h));
401
402 e1h->cardnr = cardnr;
403 e1h->bts = bts;
Harald Weltead384642008-12-26 10:20:07 +0000404 e1h->cb = cb;
Harald Welte8470bf22008-12-25 23:28:35 +0000405 INIT_LLIST_HEAD(&e1h->oml_tx_list);
406 INIT_LLIST_HEAD(&e1h->rsl_tx_list);
407
408 global_e1h = e1h;
409
410 return mi_e1_setup(e1h);
411}