blob: bfccbc768a08c4290d352b92eb09b49022ec522b [file] [log] [blame]
Harald Welte5fd8a542009-02-13 02:43:36 +00001/* OpenBSC Abis input driver for ip.access */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 *
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 <time.h>
29#include <sys/fcntl.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/ioctl.h>
33#include <arpa/inet.h>
34
35#include <openbsc/select.h>
36#include <openbsc/msgb.h>
37#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
39#include <openbsc/abis_nm.h>
40#include <openbsc/abis_rsl.h>
41#include <openbsc/subchan_demux.h>
42#include <openbsc/e1_input.h>
43
44/* data structure for one E1 interface with A-bis */
45struct ia_e1_handle {
46 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000047 struct bsc_fd rsl_listen_fd;
Harald Welte5fd8a542009-02-13 02:43:36 +000048};
49
50#define TS1_ALLOC_SIZE 300
51
52struct ipaccess_head {
53 u_int8_t zero;
54 u_int8_t len;
55 u_int8_t proto;
56 u_int8_t data[0];
57} __attribute__ ((packed));
58
59enum ipaccess_proto {
60 PROTO_RSL = 0x00,
61 PROTO_IPACCESS = 0xfe,
62 PROTO_OML = 0xff,
63};
64
65enum ipaccess_msg_type {
66 MSGT_PING = 0x00,
67 MSGT_PONG = 0x01,
68 MSGT_IDENTITY_GET = 0x04,
69 MSGT_IDENTITY_RESP = 0x05,
70 MSGT_IDENTITY_ACK = 0x06,
71};
72
73static const u_int8_t pong[] = { 0, 1, PROTO_IPACCESS, MSGT_PONG };
74static const u_int8_t id_ack[] = { 0, 1, PROTO_IPACCESS, MSGT_IDENTITY_ACK };
75
76static int ipaccess_rcvmsg(struct msgb *msg, int fd)
77{
78 u_int8_t msg_type = *(msg->l2h);
79
Harald Welte5fd8a542009-02-13 02:43:36 +000080 switch (msg_type) {
81 case MSGT_PING:
Harald Welte5fd8a542009-02-13 02:43:36 +000082 write(fd, pong, sizeof(pong));
83 break;
84 case MSGT_PONG:
85 DEBUGP(DMI, "PONG!\n");
86 break;
87 case MSGT_IDENTITY_RESP:
88 DEBUGP(DMI, "ID_RESP\n");
89 break;
90 case MSGT_IDENTITY_ACK:
Harald Welte7782c142009-02-15 03:39:51 +000091 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Harald Welte5fd8a542009-02-13 02:43:36 +000092 write(fd, id_ack, sizeof(id_ack));
93 break;
94 }
95
96 msgb_free(msg);
97 return 0;
98}
99
Harald Welte7782c142009-02-15 03:39:51 +0000100/* FIXME: this is per BTS */
101static int oml_up = 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000102static int rsl_up = 0;
Harald Welte7782c142009-02-15 03:39:51 +0000103
Harald Welte5fd8a542009-02-13 02:43:36 +0000104static int handle_ts1_read(struct bsc_fd *bfd)
105{
106 struct e1inp_line *line = bfd->data;
107 unsigned int ts_nr = bfd->priv_nr;
108 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
109 struct e1inp_sign_link *link;
110 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
111 struct ipaccess_head *hh;
112 int ret;
113
114 if (!msg)
115 return -ENOMEM;
116
117 /* first read our 3-byte header */
118 hh = (struct ipaccess_head *) msg->data;
119 ret = recv(bfd->fd, msg->data, 3, 0);
120 if (ret < 0) {
121 fprintf(stderr, "recv error %s\n", strerror(errno));
122 return ret;
123 }
124 if (ret == 0) {
125 fprintf(stderr, "BTS disappeared, dead socket\n");
Harald Welte7782c142009-02-15 03:39:51 +0000126 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, PROTO_RSL);
127 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, PROTO_OML);
Harald Welte5fd8a542009-02-13 02:43:36 +0000128 bsc_unregister_fd(bfd);
129 close(bfd->fd);
130 bfd->fd = -1;
131 }
132 msgb_put(msg, ret);
133
134 /* then read te length as specified in header */
135 msg->l2h = msg->data + sizeof(*hh);
136 ret = recv(bfd->fd, msg->l2h, hh->len, 0);
137 if (ret < hh->len) {
138 fprintf(stderr, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000139 msgb_free(msg);
140 return -EIO;
Harald Welte5fd8a542009-02-13 02:43:36 +0000141 }
142 msgb_put(msg, ret);
Harald Welte5fd8a542009-02-13 02:43:36 +0000143
144 if (hh->proto == PROTO_IPACCESS)
145 return ipaccess_rcvmsg(msg, bfd->fd);
146
147 if (debug_mask & DMI) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000148 fprintf(stdout, "RX %u: ", ts_nr);
Harald Welte5fd8a542009-02-13 02:43:36 +0000149 hexdump(msgb_l2(msg), ret);
150 }
151
152 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
153 if (!link) {
154 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
155 msgb_free(msg);
156 return -EIO;
157 }
158 msg->trx = link->trx;
159
160 switch (hh->proto) {
161 case PROTO_RSL:
Harald Welte5c1e4582009-02-15 11:57:29 +0000162 if (!rsl_up) {
163 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, PROTO_RSL);
164 rsl_up = 1;
165 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000166 ret = abis_rsl_rcvmsg(msg);
167 break;
168 case PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000169 if (!oml_up) {
170 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, PROTO_OML);
171 oml_up = 1;
172 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000173 ret = abis_nm_rcvmsg(msg);
174 break;
175 default:
Harald Welte7782c142009-02-15 03:39:51 +0000176 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000177 msgb_free(msg);
178 break;
179 }
180 return ret;
181}
182
183static int handle_ts1_write(struct bsc_fd *bfd)
184{
185 struct e1inp_line *line = bfd->data;
186 unsigned int ts_nr = bfd->priv_nr;
187 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
188 struct e1inp_sign_link *sign_link;
189 struct msgb *msg;
190 struct ipaccess_head *hh;
191 u_int8_t *l2_data;
192 int ret;
193
194 /* get the next msg for this timeslot */
195 msg = e1inp_tx_ts(e1i_ts, &sign_link);
196 if (!msg) {
197 bfd->when &= ~BSC_FD_WRITE;
198 return 0;
199 }
200
201 l2_data = msg->data;
202
203 /* prepend the mISDNhead */
204 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
205 hh->zero = 0;
206 hh->len = msg->len - sizeof(*hh);
207
208 switch (sign_link->type) {
209 case E1INP_SIGN_OML:
210 hh->proto = PROTO_OML;
211 break;
212 case E1INP_SIGN_RSL:
213 hh->proto = PROTO_RSL;
214 break;
215 default:
216 msgb_free(msg);
217 return -EINVAL;
218 }
219
220 if (debug_mask & DMI) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000221 fprintf(stdout, "TX %u: ", ts_nr);
Harald Welte5fd8a542009-02-13 02:43:36 +0000222 hexdump(l2_data, hh->len);
223 }
224
225 ret = send(bfd->fd, msg->data, msg->len, 0);
226 msgb_free(msg);
Harald Welte7782c142009-02-15 03:39:51 +0000227 usleep(100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000228
229 return ret;
230}
231
232
233/* callback from select.c in case one of the fd's can be read/written */
234static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
235{
236 struct e1inp_line *line = bfd->data;
237 unsigned int ts_nr = bfd->priv_nr;
238 unsigned int idx = ts_nr-1;
239 struct e1inp_ts *e1i_ts = &line->ts[idx];
240 int rc = 0;
241
242 switch (e1i_ts->type) {
243 case E1INP_TS_TYPE_SIGN:
244 if (what & BSC_FD_READ)
245 rc = handle_ts1_read(bfd);
246 if (what & BSC_FD_WRITE)
247 rc = handle_ts1_write(bfd);
248 break;
249#if 0
250 case E1INP_TS_TYPE_TRAU:
251 if (what & BSC_FD_READ)
252 rc = handle_tsX_read(bfd);
253 /* We never include the mISDN B-Channel FD into the
254 * writeset, since it doesn't support poll() based
255 * write flow control */
256 break;
257#endif
258 default:
259 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
260 break;
261 }
262
263 return rc;
264}
265
266
267static int ts_want_write(struct e1inp_ts *e1i_ts)
268{
269 /* We never include the mISDN B-Channel FD into the
270 * writeset, since it doesn't support poll() based
271 * write flow control */
272 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
273 return 0;
274
275 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
276
277 return 0;
278}
279
280struct e1inp_driver ipaccess_driver = {
281 .name = "ip.access",
282 .want_write = ts_want_write,
283};
284
285static int ia_e1_setup(struct e1inp_line *line)
286{
287 return 0;
288}
289
290static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
291{
292 struct e1inp_line *line = listen_bfd->data;
293 int ret;
294
295 if (what & BSC_FD_READ) {
296 int idx = 0;
297 struct e1inp_ts *e1i_ts = &line->ts[idx];
298 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
299 struct sockaddr_in sa;
300 socklen_t sa_len = sizeof(sa);
301
302 if (bfd->fd) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000303 printf("dumping old OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000304 if (bfd->fd != -1) {
305 bsc_unregister_fd(bfd);
306 close(bfd->fd);
307 }
308 }
309 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
310 if (bfd->fd < 0) {
311 perror("accept");
312 return bfd->fd;
313 }
Harald Welte5c1e4582009-02-15 11:57:29 +0000314 printf("accept()ed new OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000315 bfd->data = line;
316 bfd->priv_nr = 1;
317 bfd->cb = ipaccess_fd_cb;
318 bfd->when = BSC_FD_READ;
319 ret = bsc_register_fd(bfd);
320 if (ret < 0) {
321 fprintf(stderr, "could not register FD\n");
322 return ret;
323 }
324 }
325 return 0;
326}
327
Harald Welte5c1e4582009-02-15 11:57:29 +0000328static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
329{
330 struct e1inp_line *line = listen_bfd->data;
331 int ret;
332
333 if (what & BSC_FD_READ) {
334 int idx = 1;
335 struct e1inp_ts *e1i_ts = &line->ts[idx];
336 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
337 struct sockaddr_in sa;
338 socklen_t sa_len = sizeof(sa);
339
340 if (bfd->fd) {
341 printf("dumping old RSL fd\n");
342 if (bfd->fd != -1) {
343 bsc_unregister_fd(bfd);
344 close(bfd->fd);
345 }
346 }
347 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
348 if (bfd->fd < 0) {
349 perror("accept");
350 return bfd->fd;
351 }
352 printf("accept()ed new RSL fd\n");
353 bfd->data = line;
354 bfd->priv_nr = 2;
355 bfd->cb = ipaccess_fd_cb;
356 bfd->when = BSC_FD_READ;
357 ret = bsc_register_fd(bfd);
358 if (ret < 0) {
359 fprintf(stderr, "could not register FD\n");
360 return ret;
361 }
362 }
363 return 0;
364}
365
366static int make_sock(struct bsc_fd *bfd, u_int16_t port,
367 struct e1inp_line *line,
368 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000369{
370 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000371 int ret, on = 1;
372
373 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
374 bfd->cb = cb;
375 bfd->when = BSC_FD_READ;
376 bfd->data = line;
377
378 memset(&addr, 0, sizeof(addr));
379 addr.sin_family = AF_INET;
380 addr.sin_port = htons(port);
381 addr.sin_addr.s_addr = INADDR_ANY;
382
383 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
384
385 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
386 if (ret < 0) {
387 fprintf(stderr, "could not bind l2 socket %s\n",
388 strerror(errno));
389 return -EIO;
390 }
391
392 ret = listen(bfd->fd, 1);
393 if (ret < 0) {
394 perror("listen");
395 return ret;
396 }
397
398 ret = bsc_register_fd(bfd);
399 if (ret < 0) {
400 perror("register_listen_fd");
401 return ret;
402 }
403 return 0;
404}
405
406int ipaccess_setup(struct e1inp_line *line)
407{
Harald Welte5fd8a542009-02-13 02:43:36 +0000408 struct ia_e1_handle *e1h;
Harald Welte5c1e4582009-02-15 11:57:29 +0000409 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000410
411 /* register the driver with the core */
412 /* FIXME: do this in the plugin initializer function */
413 ret = e1inp_driver_register(&ipaccess_driver);
414 if (ret)
415 return ret;
416
417 /* create the actual line instance */
418 /* FIXME: do this independent of driver registration */
419 e1h = malloc(sizeof(*e1h));
420 memset(e1h, 0, sizeof(*e1h));
421
422 line->driver = &ipaccess_driver;
423 line->driver_data = e1h;
424
Harald Welte5c1e4582009-02-15 11:57:29 +0000425 /* Listen for OML connections */
426 ret = make_sock(&e1h->listen_fd, 3002, line, listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000427
Harald Welte5c1e4582009-02-15 11:57:29 +0000428 /* Listen for RSL connections */
429 ret = make_sock(&e1h->rsl_listen_fd, 3003, line, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000430
431 ret = ia_e1_setup(line);
432 if (ret)
433 return ret;
434
435 return e1inp_line_register(line);
436}