blob: 920349ea9ff3fd6b923f40b76c6a64701dccef0a [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>
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte52b1f982008-12-23 20:25:15 +00005 *
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>
Holger Freyther2139b242009-01-02 01:17:48 +000029#include <time.h>
Harald Weltee9a82612008-12-27 16:45:29 +000030#include <sys/fcntl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
Harald Welte4e0d1372009-01-03 03:04:07 +000034#include <arpa/inet.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035#include <mISDNif.h>
36
Harald Welte8470bf22008-12-25 23:28:35 +000037//#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>
Harald Welte52b1f982008-12-23 20:25:15 +000048
49#define NUM_E1_TS 32
50
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000051
52/*
53 * pcap writing of the misdn load
54 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
55 */
Harald Welte62a923d2009-01-03 01:32:00 +000056#define DLT_LINUX_LAPD 177
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000057#define PCAP_INPUT 0
58#define PCAP_OUTPUT 1
59
60struct pcap_hdr {
61 u_int32_t magic_number;
62 u_int16_t version_major;
63 u_int16_t version_minor;
64 int32_t thiszone;
65 u_int32_t sigfigs;
66 u_int32_t snaplen;
67 u_int32_t network;
68} __attribute__((packed));
69
70struct pcaprec_hdr {
71 u_int32_t ts_sec;
72 u_int32_t ts_usec;
73 u_int32_t incl_len;
74 u_int32_t orig_len;
75} __attribute__((packed));
76
Harald Welte4e0d1372009-01-03 03:04:07 +000077struct fake_linux_lapd_header {
78 u_int16_t pkttype;
79 u_int16_t hatype;
80 u_int16_t halen;
81 u_int64_t addr;
82 int16_t protocol;
Harald Welte62a923d2009-01-03 01:32:00 +000083} __attribute__((packed));
84
Harald Welte599639e2009-01-03 03:28:10 +000085struct lapd_header {
86 u_int8_t ea1 : 1;
87 u_int8_t cr : 1;
88 u_int8_t sapi : 6;
89 u_int8_t ea2 : 1;
90 u_int8_t tei : 7;
91 u_int8_t control_foo; /* fake UM's ... */
92} __attribute__((packed));
93
Harald Welte4e0d1372009-01-03 03:04:07 +000094static_assert((int)&((struct fake_linux_lapd_header*)NULL)->hatype == 2, hatype_offset);
95static_assert((int)&((struct fake_linux_lapd_header*)NULL)->halen == 4, halen_offset);
96static_assert((int)&((struct fake_linux_lapd_header*)NULL)->addr == 6, addr_offset);
97static_assert((int)&((struct fake_linux_lapd_header*)NULL)->protocol == 14, proto_offset);
98static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
99
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000100
101static int pcap_fd = -1;
102
103void mi_set_pcap_fd(int fd)
104{
105 int ret;
106 struct pcap_hdr header = {
107 .magic_number = 0xa1b2c3d4,
108 .version_major = 2,
109 .version_minor = 4,
110 .thiszone = 0,
111 .sigfigs = 0,
112 .snaplen = 65535,
Harald Welte62a923d2009-01-03 01:32:00 +0000113 .network = DLT_LINUX_LAPD,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000114 };
115
116 pcap_fd = fd;
117 ret = write(pcap_fd, &header, sizeof(header));
118}
119
Harald Welte62a923d2009-01-03 01:32:00 +0000120/* This currently only works for the D-Channel */
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000121static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr,
122 struct msgb *msg) {
123 if (pcap_fd < 0)
124 return;
125
126 int ret;
Holger Freyther2139b242009-01-02 01:17:48 +0000127 time_t cur_time;
128 struct tm *tm;
Harald Welte62a923d2009-01-03 01:32:00 +0000129
Harald Welte4e0d1372009-01-03 03:04:07 +0000130 struct fake_linux_lapd_header header = {
131 .pkttype = 4,
132 .hatype = 0,
133 .halen = 0,
Harald Welte599639e2009-01-03 03:28:10 +0000134 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
Harald Welte4e0d1372009-01-03 03:04:07 +0000135 .protocol = ntohs(48),
Harald Welte62a923d2009-01-03 01:32:00 +0000136 };
Harald Welte599639e2009-01-03 03:28:10 +0000137
138 struct lapd_header lapd_header = {
139 .ea1 = 0,
140 .cr = direction == PCAP_OUTPUT ? 1 : 0,
141 .sapi = addr->sapi & 0x3F,
142 .ea2 = 1,
143 .tei = addr->tei & 0x7F,
144 .control_foo = 0x13 /* UI with P set */,
145 };
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000146
147 struct pcaprec_hdr payload_header = {
148 .ts_sec = 0,
149 .ts_usec = 0,
Harald Welte4e0d1372009-01-03 03:04:07 +0000150 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
Harald Welte599639e2009-01-03 03:28:10 +0000151 + sizeof(struct lapd_header)
Harald Welte62a923d2009-01-03 01:32:00 +0000152 - MISDN_HEADER_LEN,
Harald Welte4e0d1372009-01-03 03:04:07 +0000153 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
Harald Welte599639e2009-01-03 03:28:10 +0000154 + sizeof(struct lapd_header)
Harald Welte62a923d2009-01-03 01:32:00 +0000155 - MISDN_HEADER_LEN,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000156 };
157
Harald Welte62a923d2009-01-03 01:32:00 +0000158
Holger Freyther2139b242009-01-02 01:17:48 +0000159 cur_time = time(NULL);
160 tm = localtime(&cur_time);
161 payload_header.ts_sec = mktime(tm);
162
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000163 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
Harald Welteca6bf782009-01-03 00:41:04 +0000164 ret = write(pcap_fd, &header, sizeof(header));
Harald Welte599639e2009-01-03 03:28:10 +0000165 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000166 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
167 msg->len - MISDN_HEADER_LEN);
168}
169
Harald Welte52b1f982008-12-23 20:25:15 +0000170/* data structure for one E1 interface with A-bis */
171struct mi_e1_handle {
172 struct gsm_bts *bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000173 /* The mISDN card number of the card we use */
174 int cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000175 /* The RSL adress */
176 struct sockaddr_mISDN l2addr;
Harald Welte52b1f982008-12-23 20:25:15 +0000177 /* The OML adress */
178 struct sockaddr_mISDN omladdr;
Harald Welte8470bf22008-12-25 23:28:35 +0000179 /* list (queue) of to-be-sent msgb's */
180 struct llist_head rsl_tx_list;
181 struct llist_head oml_tx_list;
Harald Welte52b1f982008-12-23 20:25:15 +0000182
Harald Weltead384642008-12-26 10:20:07 +0000183 void (*cb)(int event, struct gsm_bts *bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000184 struct bsc_fd fd[NUM_E1_TS];
Harald Weltee9a82612008-12-27 16:45:29 +0000185
186 int ts2_fd;
Harald Welte52b1f982008-12-23 20:25:15 +0000187};
188
Harald Welte8470bf22008-12-25 23:28:35 +0000189/* FIXME: this needs to go */
190static struct mi_e1_handle *global_e1h;
191
Harald Welte52b1f982008-12-23 20:25:15 +0000192#define SAPI_L2ML 0
193#define SAPI_OML 62
Harald Weltead384642008-12-26 10:20:07 +0000194#define SAPI_RSL 0 /* 63 ? */
Harald Welte52b1f982008-12-23 20:25:15 +0000195
196#define TEI_L2ML 127
197#define TEI_OML 25
198#define TEI_RSL 1
199
Harald Welte702d8702008-12-26 20:25:35 +0000200void hexdump(unsigned char *buf, int len)
Harald Welte52b1f982008-12-23 20:25:15 +0000201{
202 int i;
203 for (i = 0; i < len; i++) {
204 fprintf(stdout, "%02x ", buf[i]);
205 }
206 fprintf(stdout, "\n");
207}
208
209#define TS1_ALLOC_SIZE 300
210
211static int handle_ts1_read(struct bsc_fd *bfd)
212{
213 struct mi_e1_handle *e1h = bfd->data;
214 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
Harald Welte8470bf22008-12-25 23:28:35 +0000215 struct sockaddr_mISDN l2addr;
216 struct mISDNhead *hh;
Harald Welte52b1f982008-12-23 20:25:15 +0000217 socklen_t alen;
Harald Welte8470bf22008-12-25 23:28:35 +0000218 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000219
220 if (!msg)
221 return -ENOMEM;
222
Harald Welte8470bf22008-12-25 23:28:35 +0000223 hh = (struct mISDNhead *) msg->data;
224
225 /* FIXME: Map TEI/SAPI to TRX */
226 msg->trx = e1h->bts->c0;
Harald Welte52b1f982008-12-23 20:25:15 +0000227
228 alen = sizeof(l2addr);
229 ret = recvfrom(bfd->fd, msg->data, 300, 0,
230 (struct sockaddr *) &l2addr, &alen);
231 if (ret < 0) {
232 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
233 return ret;
234 }
235
236 if (alen != sizeof(l2addr))
237 return -EINVAL;
238
239 msgb_put(msg, ret);
240
241 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
242 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
243
244 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n",
245 ret, hh->prim, hh->id);
246
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000247
Harald Welte52b1f982008-12-23 20:25:15 +0000248 switch (hh->prim) {
249 case DL_INFORMATION_IND:
250 DEBUGP(DMI, "got DL_INFORMATION_IND\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000251 struct sockaddr_mISDN *sa = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000252 char *lstr = "UNKN";
253
254 switch (l2addr.tei) {
255 case TEI_OML:
256 sa = &e1h->omladdr;
257 lstr = "OML";
258 break;
259 case TEI_RSL:
260 sa = &e1h->l2addr;
261 lstr = "RSL";
262 break;
263 default:
Harald Welte8470bf22008-12-25 23:28:35 +0000264 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000265 }
Harald Welte8470bf22008-12-25 23:28:35 +0000266 if (sa) {
267 DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n",
268 lstr, l2addr.channel, l2addr.sapi, l2addr.tei);
269 memcpy(sa, &l2addr, sizeof(l2addr));
270 }
Harald Welte52b1f982008-12-23 20:25:15 +0000271 break;
272 case DL_ESTABLISH_IND:
273 DEBUGP(DMI, "got DL_ESTABLISH_IND\n");
274 break;
275 case DL_ESTABLISH_CNF:
276 DEBUGP(DMI, "got DL_ESTABLISH_CNF\n");
277 break;
278 case DL_RELEASE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000279 DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000280 break;
281 case MPH_ACTIVATE_IND:
282 DEBUGP(DMI, "got MPH_ACTIVATE_IND\n");
Harald Weltead384642008-12-26 10:20:07 +0000283 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
284 e1h->cb(EVT_E1_OML_UP, e1h->bts);
285 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
286 e1h->cb(EVT_E1_RSL_UP, e1h->bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000287 break;
288 case MPH_DEACTIVATE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000289 DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n");
290 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
291 e1h->cb(EVT_E1_OML_DN, e1h->bts);
292 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
293 e1h->cb(EVT_E1_RSL_DN, e1h->bts);
294 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000295 case DL_DATA_IND:
296 DEBUGP(DMI, "got DL_DATA_IND\n");
Holger Freyther52af7d42009-01-02 01:18:28 +0000297 write_pcap_packet(PCAP_INPUT, &l2addr, msg);
Harald Weltead384642008-12-26 10:20:07 +0000298
Harald Weltead384642008-12-26 10:20:07 +0000299 msg->l2h = msg->data + MISDN_HEADER_LEN;
300
Harald Welte6cc38d72008-12-30 14:58:19 +0000301 if (debug_mask & DMI) {
302 fprintf(stdout, "RX: ");
303 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
304 }
Harald Welte52b1f982008-12-23 20:25:15 +0000305 switch (l2addr.tei) {
306 case TEI_OML:
307 ret = abis_nm_rcvmsg(msg);
308 break;
309 case TEI_RSL:
310 ret = abis_rsl_rcvmsg(msg);
311 break;
312 default:
313 fprintf(stderr, "DATA_IND for unknown TEI\n");
314 break;
315 }
316 break;
317 default:
318 DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim);
319 break;
320 }
321 return ret;
322}
323
324static int handle_ts1_write(struct bsc_fd *bfd)
325{
326 struct mi_e1_handle *e1h = bfd->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000327 struct msgb *msg;
328 struct mISDNhead *hh;
Harald Weltead384642008-12-26 10:20:07 +0000329 int ret, no_oml = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000330
Harald Weltead384642008-12-26 10:20:07 +0000331 msg = msgb_dequeue(&e1h->oml_tx_list);
Harald Welte8470bf22008-12-25 23:28:35 +0000332 if (!msg)
Harald Weltead384642008-12-26 10:20:07 +0000333 no_oml = 1;
Harald Welte8470bf22008-12-25 23:28:35 +0000334 else {
Harald Weltead384642008-12-26 10:20:07 +0000335 u_int8_t *l2_data = msg->data;
336
Harald Welte8470bf22008-12-25 23:28:35 +0000337 /* prepend the mISDNhead */
338 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
339 hh->prim = DL_DATA_REQ;
Harald Welte52b1f982008-12-23 20:25:15 +0000340
Harald Welte6cc38d72008-12-30 14:58:19 +0000341 if (debug_mask & DMI) {
342 fprintf(stdout, "OML TX: ");
343 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
344 }
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000345
346 write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000347 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000348 (struct sockaddr *)&e1h->omladdr,
349 sizeof(e1h->omladdr));
Harald Weltee571fb82008-12-25 23:50:30 +0000350 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000351 usleep(100000);
Harald Weltead384642008-12-26 10:20:07 +0000352 /* we always dequeue all OML messages */
353 return ret;
Harald Welte8470bf22008-12-25 23:28:35 +0000354 }
Harald Weltead384642008-12-26 10:20:07 +0000355
Harald Welte8470bf22008-12-25 23:28:35 +0000356 msg = msgb_dequeue(&e1h->rsl_tx_list);
357 if (!msg) {
Harald Weltead384642008-12-26 10:20:07 +0000358 if (no_oml)
Harald Welte8470bf22008-12-25 23:28:35 +0000359 bfd->when &= ~BSC_FD_WRITE;
360 } else {
Harald Weltead384642008-12-26 10:20:07 +0000361 u_int8_t *l2_data = msg->data;
362
Harald Welte8470bf22008-12-25 23:28:35 +0000363 /* prepend the mISDNhead */
364 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
365 hh->prim = DL_DATA_REQ;
366
Harald Welte6cc38d72008-12-30 14:58:19 +0000367 if (debug_mask & DMI) {
368 fprintf(stdout, "RSL TX: ");
369 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
370 }
Harald Weltead384642008-12-26 10:20:07 +0000371
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000372 write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000373 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000374 (struct sockaddr *)&e1h->l2addr,
375 sizeof(e1h->l2addr));
Harald Weltee571fb82008-12-25 23:50:30 +0000376 msgb_free(msg);
Harald Welteb2750422008-12-27 11:24:23 +0000377 usleep(10000);
Harald Welte702d8702008-12-26 20:25:35 +0000378 //sleep(1);
Harald Welte8470bf22008-12-25 23:28:35 +0000379 }
380
381 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000382}
383
Harald Weltee9a82612008-12-27 16:45:29 +0000384#define TSX_ALLOC_SIZE 4096
385
386/* FIXME: read from a B channel TS */
Harald Welte52b1f982008-12-23 20:25:15 +0000387static int handle_tsX_read(struct bsc_fd *bfd)
388{
Harald Weltee9a82612008-12-27 16:45:29 +0000389 struct mi_e1_handle *e1h = bfd->data;
390 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
391 struct mISDNhead *hh;
392 int ret;
393
394 if (!msg)
395 return -ENOMEM;
396
397 hh = (struct mISDNhead *) msg->data;
398
399 /* FIXME: Map TEI/SAPI to TRX */
400 msg->trx = e1h->bts->c0;
401
402 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
403 if (ret < 0) {
404 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
405 return ret;
406 }
407
408 msgb_put(msg, ret);
409
410 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id);
411
412 switch (hh->prim) {
413 case PH_CONTROL_IND:
414 DEBUGP(DMIB, "got PH_CONTROL_IND\n");
415 break;
416 case PH_DATA_IND:
417 DEBUGP(DMIB, "got PH_DATA_IND\n");
418
419 msg->l2h = msg->data + MISDN_HEADER_LEN;
420
Harald Welte6cc38d72008-12-30 14:58:19 +0000421 if (debug_mask & DMIB) {
422 fprintf(stdout, "BCHAN RX: ");
423 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
424 }
Harald Weltee9a82612008-12-27 16:45:29 +0000425 if (!e1h->ts2_fd)
426 e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660);
427
428 write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN);
429
430 break;
431 default:
432 DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim);
433 break;
434 }
Harald Weltee27bb342008-12-29 06:06:35 +0000435 /* FIXME: don't free it if we still hold reference! */
436 msgb_free(msg);
437
Harald Weltee9a82612008-12-27 16:45:29 +0000438 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000439}
440
Harald Welte8470bf22008-12-25 23:28:35 +0000441static int handle_tsX_write(struct bsc_fd *bfd)
Harald Welte52b1f982008-12-23 20:25:15 +0000442{
443 /* FIXME: write to a B channel TS */
Holger Freyther059c2542008-12-27 09:39:48 +0000444 return -1;
Harald Welte52b1f982008-12-23 20:25:15 +0000445}
446
447/* callback from select.c in case one of the fd's can be read/written */
Harald Welte8470bf22008-12-25 23:28:35 +0000448static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
Harald Welte52b1f982008-12-23 20:25:15 +0000449{
450 unsigned int e1_ts = bfd->priv_nr;
451 int rc = 0;
452
453 switch (e1_ts) {
454 case 1:
455 if (what & BSC_FD_READ)
456 rc = handle_ts1_read(bfd);
457 if (what & BSC_FD_WRITE)
458 rc = handle_ts1_write(bfd);
459 break;
460 default:
461 if (what & BSC_FD_READ)
462 rc = handle_tsX_read(bfd);
463 if (what & BSC_FD_WRITE)
464 rc = handle_tsX_write(bfd);
465 break;
466 }
467
468 return rc;
469}
470
Harald Welte8470bf22008-12-25 23:28:35 +0000471int abis_rsl_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000472{
Harald Welte8470bf22008-12-25 23:28:35 +0000473 struct mi_e1_handle *e1h = global_e1h;
474
Harald Weltead384642008-12-26 10:20:07 +0000475 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000476 msgb_enqueue(&e1h->rsl_tx_list, msg);
477 e1h->fd[0].when |= BSC_FD_WRITE;
478
479 return 0;
480}
481
Harald Weltead384642008-12-26 10:20:07 +0000482int _abis_nm_sendmsg(struct msgb *msg)
Harald Welte8470bf22008-12-25 23:28:35 +0000483{
484 struct mi_e1_handle *e1h = global_e1h;
485
Harald Weltead384642008-12-26 10:20:07 +0000486 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000487 msgb_enqueue(&e1h->oml_tx_list, msg);
488 e1h->fd[0].when |= BSC_FD_WRITE;
489
490 return 0;
491}
492
Harald Weltee9a82612008-12-27 16:45:29 +0000493static int activate_bchan(struct mi_e1_handle *e1h, int ts)
494{
495 struct mISDNhead hh;
496 int ret;
497 struct bsc_fd *bfd = &e1h->fd[ts-1];
498
499 fprintf(stdout, "activate bchan\n");
500 hh.prim = PH_ACTIVATE_REQ;
501 hh.id = MISDN_ID_ANY;
502 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
503 if (ret < 0) {
504 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
505 strerror(errno));
506 return 0;
507 }
508
509 return ret;
510}
511
Harald Welte8470bf22008-12-25 23:28:35 +0000512static int mi_e1_setup(struct mi_e1_handle *e1h)
513{
514 int ts, sk, ret, cnt;
515 struct mISDN_devinfo devinfo;
Harald Welte52b1f982008-12-23 20:25:15 +0000516
517 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
Harald Welte8470bf22008-12-25 23:28:35 +0000518 if (sk < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000519 fprintf(stderr, "could not open socket %s\n", strerror(errno));
520 return sk;
521 }
522
523 ret = ioctl(sk, IMGETCOUNT, &cnt);
524 if (ret) {
525 fprintf(stderr, "error getting interf count: %s\n",
526 strerror(errno));
527 close(sk);
528 return -ENODEV;
529 }
Harald Weltead384642008-12-26 10:20:07 +0000530 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
531 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
532#if 1
533 devinfo.id = e1h->cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000534 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
535 if (ret < 0) {
536 fprintf(stdout, "error getting info for device %d: %s\n",
Harald Weltead384642008-12-26 10:20:07 +0000537 e1h->cardnr, strerror(errno));
Harald Welte52b1f982008-12-23 20:25:15 +0000538 return -ENODEV;
539 }
540 fprintf(stdout, " id: %d\n", devinfo.id);
541 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
542 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
543 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
544 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
545 fprintf(stdout, " name: %s\n", devinfo.name);
546#endif
547
548 /* TS0 is CRC4, don't need any fd for it */
549 for (ts = 1; ts < NUM_E1_TS; ts++) {
Harald Welte8470bf22008-12-25 23:28:35 +0000550 unsigned int idx = ts-1;
Harald Welte52b1f982008-12-23 20:25:15 +0000551 struct bsc_fd *bfd = &e1h->fd[idx];
552 struct sockaddr_mISDN addr;
553
Harald Welte8470bf22008-12-25 23:28:35 +0000554 bfd->data = e1h;
555 bfd->priv_nr = ts;
556 bfd->cb = misdn_fd_cb;
557
558 if (ts == 1) {
Harald Weltead384642008-12-26 10:20:07 +0000559 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
Harald Welte8470bf22008-12-25 23:28:35 +0000560 bfd->when = BSC_FD_READ;
Harald Welte6cc38d72008-12-30 14:58:19 +0000561 } else
Harald Welte52b1f982008-12-23 20:25:15 +0000562 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
563
Harald Welte8470bf22008-12-25 23:28:35 +0000564 if (bfd->fd < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000565 fprintf(stderr, "could not open socket %s\n",
566 strerror(errno));
567 return bfd->fd;
568 }
569
570 memset(&addr, 0, sizeof(addr));
571 addr.family = AF_ISDN;
572 addr.dev = e1h->cardnr;
573 if (ts == 1) {
574 addr.channel = 0;
575 addr.sapi = 0;/* SAPI not supported yet in kernel */
576 addr.tei = TEI_L2ML;
577 } else
Harald Welte6cc38d72008-12-30 14:58:19 +0000578 addr.channel = ts;
Harald Welte52b1f982008-12-23 20:25:15 +0000579
580 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
581 if (ret < 0) {
Harald Welte8470bf22008-12-25 23:28:35 +0000582 fprintf(stderr, "could not bind l2 socket %s\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000583 strerror(errno));
584 return -EIO;
585 }
Harald Welte8470bf22008-12-25 23:28:35 +0000586
Harald Weltee9a82612008-12-27 16:45:29 +0000587 if (ts == 2) {
588 bfd->when = BSC_FD_READ;
589 activate_bchan(e1h, ts);
590 }
591
Harald Welte8470bf22008-12-25 23:28:35 +0000592 ret = bsc_register_fd(bfd);
593 if (ret < 0) {
594 fprintf(stderr, "could not register FD: %s\n",
595 strerror(ret));
596 return ret;
597 }
Harald Welte52b1f982008-12-23 20:25:15 +0000598 }
Harald Welte8470bf22008-12-25 23:28:35 +0000599
600 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000601}
602
Harald Weltead384642008-12-26 10:20:07 +0000603int mi_setup(struct gsm_bts *bts, int cardnr,
604 void (cb)(int event, struct gsm_bts *bts))
Harald Welte8470bf22008-12-25 23:28:35 +0000605{
606 struct mi_e1_handle *e1h;
607
608 e1h = malloc(sizeof(*e1h));
609 memset(e1h, 0, sizeof(*e1h));
610
611 e1h->cardnr = cardnr;
612 e1h->bts = bts;
Harald Weltead384642008-12-26 10:20:07 +0000613 e1h->cb = cb;
Harald Welte8470bf22008-12-25 23:28:35 +0000614 INIT_LLIST_HEAD(&e1h->oml_tx_list);
615 INIT_LLIST_HEAD(&e1h->rsl_tx_list);
616
617 global_e1h = e1h;
618
619 return mi_e1_setup(e1h);
620}