blob: b4d53bd8f4967c298b9251ac5e38e927e0dcc8d0 [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>
Harald Welte4f361fc2009-02-15 15:32:53 +000043#include <openbsc/ipaccess.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000044
45/* data structure for one E1 interface with A-bis */
46struct ia_e1_handle {
47 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000048 struct bsc_fd rsl_listen_fd;
Harald Welte5fd8a542009-02-13 02:43:36 +000049};
50
51#define TS1_ALLOC_SIZE 300
52
Harald Welte4f361fc2009-02-15 15:32:53 +000053static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
54static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
Harald Welte5fd8a542009-02-13 02:43:36 +000055
56static int ipaccess_rcvmsg(struct msgb *msg, int fd)
57{
58 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +000059 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +000060
Harald Welte5fd8a542009-02-13 02:43:36 +000061 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +000062 case IPAC_MSGT_PING:
Holger Freytherff9592f2009-03-09 16:17:14 +000063 ret = write(fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +000064 break;
Harald Welte4f361fc2009-02-15 15:32:53 +000065 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +000066 DEBUGP(DMI, "PONG!\n");
67 break;
Harald Welte4f361fc2009-02-15 15:32:53 +000068 case IPAC_MSGT_ID_RESP:
Harald Welte5fd8a542009-02-13 02:43:36 +000069 DEBUGP(DMI, "ID_RESP\n");
70 break;
Harald Welte4f361fc2009-02-15 15:32:53 +000071 case IPAC_MSGT_ID_ACK:
Harald Welte7782c142009-02-15 03:39:51 +000072 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Freytherff9592f2009-03-09 16:17:14 +000073 ret = write(fd, id_ack, sizeof(id_ack));
Harald Welte5fd8a542009-02-13 02:43:36 +000074 break;
75 }
76
77 msgb_free(msg);
78 return 0;
79}
80
Harald Welte7782c142009-02-15 03:39:51 +000081/* FIXME: this is per BTS */
82static int oml_up = 0;
Harald Welte5c1e4582009-02-15 11:57:29 +000083static int rsl_up = 0;
Harald Welte7782c142009-02-15 03:39:51 +000084
Harald Welte5fd8a542009-02-13 02:43:36 +000085static int handle_ts1_read(struct bsc_fd *bfd)
86{
87 struct e1inp_line *line = bfd->data;
88 unsigned int ts_nr = bfd->priv_nr;
89 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
90 struct e1inp_sign_link *link;
91 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
92 struct ipaccess_head *hh;
93 int ret;
94
95 if (!msg)
96 return -ENOMEM;
97
98 /* first read our 3-byte header */
99 hh = (struct ipaccess_head *) msg->data;
100 ret = recv(bfd->fd, msg->data, 3, 0);
101 if (ret < 0) {
102 fprintf(stderr, "recv error %s\n", strerror(errno));
103 return ret;
104 }
105 if (ret == 0) {
106 fprintf(stderr, "BTS disappeared, dead socket\n");
Harald Welte4f361fc2009-02-15 15:32:53 +0000107 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
108 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
Harald Welte5fd8a542009-02-13 02:43:36 +0000109 bsc_unregister_fd(bfd);
110 close(bfd->fd);
111 bfd->fd = -1;
112 }
113 msgb_put(msg, ret);
114
115 /* then read te length as specified in header */
116 msg->l2h = msg->data + sizeof(*hh);
117 ret = recv(bfd->fd, msg->l2h, hh->len, 0);
118 if (ret < hh->len) {
119 fprintf(stderr, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000120 msgb_free(msg);
121 return -EIO;
Harald Welte5fd8a542009-02-13 02:43:36 +0000122 }
123 msgb_put(msg, ret);
Harald Welte5fd8a542009-02-13 02:43:36 +0000124
Harald Welte4f361fc2009-02-15 15:32:53 +0000125 if (hh->proto == IPAC_PROTO_IPACCESS)
Harald Welte5fd8a542009-02-13 02:43:36 +0000126 return ipaccess_rcvmsg(msg, bfd->fd);
127
Harald Welte3cc4bf52009-02-28 13:08:01 +0000128 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), ret));
Harald Welte5fd8a542009-02-13 02:43:36 +0000129
130 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
131 if (!link) {
132 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
133 msgb_free(msg);
134 return -EIO;
135 }
136 msg->trx = link->trx;
137
138 switch (hh->proto) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000139 case IPAC_PROTO_RSL:
Harald Welte5c1e4582009-02-15 11:57:29 +0000140 if (!rsl_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000141 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_RSL);
Harald Welte5c1e4582009-02-15 11:57:29 +0000142 rsl_up = 1;
143 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000144 ret = abis_rsl_rcvmsg(msg);
145 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000146 case IPAC_PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000147 if (!oml_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000148 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_OML);
Harald Welte7782c142009-02-15 03:39:51 +0000149 oml_up = 1;
150 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000151 ret = abis_nm_rcvmsg(msg);
152 break;
153 default:
Harald Welte7782c142009-02-15 03:39:51 +0000154 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000155 msgb_free(msg);
156 break;
157 }
158 return ret;
159}
160
161static int handle_ts1_write(struct bsc_fd *bfd)
162{
163 struct e1inp_line *line = bfd->data;
164 unsigned int ts_nr = bfd->priv_nr;
165 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
166 struct e1inp_sign_link *sign_link;
167 struct msgb *msg;
168 struct ipaccess_head *hh;
169 u_int8_t *l2_data;
170 int ret;
171
172 /* get the next msg for this timeslot */
173 msg = e1inp_tx_ts(e1i_ts, &sign_link);
174 if (!msg) {
175 bfd->when &= ~BSC_FD_WRITE;
176 return 0;
177 }
178
179 l2_data = msg->data;
180
181 /* prepend the mISDNhead */
182 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
183 hh->zero = 0;
184 hh->len = msg->len - sizeof(*hh);
185
186 switch (sign_link->type) {
187 case E1INP_SIGN_OML:
Harald Welte4f361fc2009-02-15 15:32:53 +0000188 hh->proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000189 break;
190 case E1INP_SIGN_RSL:
Harald Welte4f361fc2009-02-15 15:32:53 +0000191 hh->proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000192 break;
193 default:
194 msgb_free(msg);
195 return -EINVAL;
196 }
197
Harald Welte3cc4bf52009-02-28 13:08:01 +0000198 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(l2_data, hh->len));
Harald Welte5fd8a542009-02-13 02:43:36 +0000199
200 ret = send(bfd->fd, msg->data, msg->len, 0);
201 msgb_free(msg);
Harald Welte7782c142009-02-15 03:39:51 +0000202 usleep(100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000203
204 return ret;
205}
206
207
208/* callback from select.c in case one of the fd's can be read/written */
209static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
210{
211 struct e1inp_line *line = bfd->data;
212 unsigned int ts_nr = bfd->priv_nr;
213 unsigned int idx = ts_nr-1;
214 struct e1inp_ts *e1i_ts = &line->ts[idx];
215 int rc = 0;
216
217 switch (e1i_ts->type) {
218 case E1INP_TS_TYPE_SIGN:
219 if (what & BSC_FD_READ)
220 rc = handle_ts1_read(bfd);
221 if (what & BSC_FD_WRITE)
222 rc = handle_ts1_write(bfd);
223 break;
224#if 0
225 case E1INP_TS_TYPE_TRAU:
226 if (what & BSC_FD_READ)
227 rc = handle_tsX_read(bfd);
228 /* We never include the mISDN B-Channel FD into the
229 * writeset, since it doesn't support poll() based
230 * write flow control */
231 break;
232#endif
233 default:
234 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
235 break;
236 }
237
238 return rc;
239}
240
241
242static int ts_want_write(struct e1inp_ts *e1i_ts)
243{
244 /* We never include the mISDN B-Channel FD into the
245 * writeset, since it doesn't support poll() based
246 * write flow control */
247 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
248 return 0;
249
250 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
251
252 return 0;
253}
254
255struct e1inp_driver ipaccess_driver = {
256 .name = "ip.access",
257 .want_write = ts_want_write,
258};
259
260static int ia_e1_setup(struct e1inp_line *line)
261{
262 return 0;
263}
264
265static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
266{
267 struct e1inp_line *line = listen_bfd->data;
268 int ret;
269
270 if (what & BSC_FD_READ) {
271 int idx = 0;
272 struct e1inp_ts *e1i_ts = &line->ts[idx];
273 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
274 struct sockaddr_in sa;
275 socklen_t sa_len = sizeof(sa);
276
277 if (bfd->fd) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000278 printf("dumping old OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000279 if (bfd->fd != -1) {
280 bsc_unregister_fd(bfd);
281 close(bfd->fd);
282 }
283 }
284 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
285 if (bfd->fd < 0) {
286 perror("accept");
287 return bfd->fd;
288 }
Harald Welte5c1e4582009-02-15 11:57:29 +0000289 printf("accept()ed new OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000290 bfd->data = line;
291 bfd->priv_nr = 1;
292 bfd->cb = ipaccess_fd_cb;
293 bfd->when = BSC_FD_READ;
294 ret = bsc_register_fd(bfd);
295 if (ret < 0) {
296 fprintf(stderr, "could not register FD\n");
297 return ret;
298 }
299 }
300 return 0;
301}
302
Harald Welte5c1e4582009-02-15 11:57:29 +0000303static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
304{
305 struct e1inp_line *line = listen_bfd->data;
306 int ret;
307
308 if (what & BSC_FD_READ) {
309 int idx = 1;
310 struct e1inp_ts *e1i_ts = &line->ts[idx];
311 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
312 struct sockaddr_in sa;
313 socklen_t sa_len = sizeof(sa);
314
315 if (bfd->fd) {
316 printf("dumping old RSL fd\n");
317 if (bfd->fd != -1) {
318 bsc_unregister_fd(bfd);
319 close(bfd->fd);
320 }
321 }
322 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
323 if (bfd->fd < 0) {
324 perror("accept");
325 return bfd->fd;
326 }
327 printf("accept()ed new RSL fd\n");
328 bfd->data = line;
329 bfd->priv_nr = 2;
330 bfd->cb = ipaccess_fd_cb;
331 bfd->when = BSC_FD_READ;
332 ret = bsc_register_fd(bfd);
333 if (ret < 0) {
334 fprintf(stderr, "could not register FD\n");
335 return ret;
336 }
337 }
338 return 0;
339}
340
341static int make_sock(struct bsc_fd *bfd, u_int16_t port,
342 struct e1inp_line *line,
343 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000344{
345 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000346 int ret, on = 1;
347
348 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
349 bfd->cb = cb;
350 bfd->when = BSC_FD_READ;
351 bfd->data = line;
352
353 memset(&addr, 0, sizeof(addr));
354 addr.sin_family = AF_INET;
355 addr.sin_port = htons(port);
356 addr.sin_addr.s_addr = INADDR_ANY;
357
358 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
359
360 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
361 if (ret < 0) {
362 fprintf(stderr, "could not bind l2 socket %s\n",
363 strerror(errno));
364 return -EIO;
365 }
366
367 ret = listen(bfd->fd, 1);
368 if (ret < 0) {
369 perror("listen");
370 return ret;
371 }
372
373 ret = bsc_register_fd(bfd);
374 if (ret < 0) {
375 perror("register_listen_fd");
376 return ret;
377 }
378 return 0;
379}
380
Harald Welte25de9912009-04-30 15:53:07 +0000381/* Actively connect to a BTS. Currently used by ipaccess-config.c */
382int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
383{
384 struct e1inp_ts *e1i_ts = &line->ts[0];
385 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
386 int ret, on = 1;
387
388 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
389 bfd->cb = ipaccess_fd_cb;
390 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
391 bfd->data = line;
392 bfd->priv_nr = 1;
393
394 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
395
396 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
397 if (ret < 0) {
398 fprintf(stderr, "could not connect socket\n");
399 close(bfd->fd);
400 return ret;
401 }
402
403 ret = bsc_register_fd(bfd);
404 if (ret < 0) {
405 close(bfd->fd);
406 return ret;
407 }
408
409 line->driver = &ipaccess_driver;
410
411 return e1inp_line_register(line);
412}
413
Harald Welte5c1e4582009-02-15 11:57:29 +0000414int ipaccess_setup(struct e1inp_line *line)
415{
Harald Welte5fd8a542009-02-13 02:43:36 +0000416 struct ia_e1_handle *e1h;
Harald Welte5c1e4582009-02-15 11:57:29 +0000417 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000418
419 /* register the driver with the core */
420 /* FIXME: do this in the plugin initializer function */
421 ret = e1inp_driver_register(&ipaccess_driver);
422 if (ret)
423 return ret;
424
425 /* create the actual line instance */
426 /* FIXME: do this independent of driver registration */
427 e1h = malloc(sizeof(*e1h));
428 memset(e1h, 0, sizeof(*e1h));
429
430 line->driver = &ipaccess_driver;
431 line->driver_data = e1h;
432
Harald Welte5c1e4582009-02-15 11:57:29 +0000433 /* Listen for OML connections */
434 ret = make_sock(&e1h->listen_fd, 3002, line, listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000435
Harald Welte5c1e4582009-02-15 11:57:29 +0000436 /* Listen for RSL connections */
437 ret = make_sock(&e1h->rsl_listen_fd, 3003, line, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000438
439 ret = ia_e1_setup(line);
440 if (ret)
441 return ret;
442
443 return e1inp_line_register(line);
444}