blob: 6c83d6d3d3059ce7974992ad1de6362ca964c4e1 [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);
59
Harald Welte5fd8a542009-02-13 02:43:36 +000060 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +000061 case IPAC_MSGT_PING:
Harald Welte5fd8a542009-02-13 02:43:36 +000062 write(fd, pong, sizeof(pong));
63 break;
Harald Welte4f361fc2009-02-15 15:32:53 +000064 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +000065 DEBUGP(DMI, "PONG!\n");
66 break;
Harald Welte4f361fc2009-02-15 15:32:53 +000067 case IPAC_MSGT_ID_RESP:
Harald Welte5fd8a542009-02-13 02:43:36 +000068 DEBUGP(DMI, "ID_RESP\n");
69 break;
Harald Welte4f361fc2009-02-15 15:32:53 +000070 case IPAC_MSGT_ID_ACK:
Harald Welte7782c142009-02-15 03:39:51 +000071 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Harald Welte5fd8a542009-02-13 02:43:36 +000072 write(fd, id_ack, sizeof(id_ack));
73 break;
74 }
75
76 msgb_free(msg);
77 return 0;
78}
79
Harald Welte7782c142009-02-15 03:39:51 +000080/* FIXME: this is per BTS */
81static int oml_up = 0;
Harald Welte5c1e4582009-02-15 11:57:29 +000082static int rsl_up = 0;
Harald Welte7782c142009-02-15 03:39:51 +000083
Harald Welte5fd8a542009-02-13 02:43:36 +000084static int handle_ts1_read(struct bsc_fd *bfd)
85{
86 struct e1inp_line *line = bfd->data;
87 unsigned int ts_nr = bfd->priv_nr;
88 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
89 struct e1inp_sign_link *link;
90 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
91 struct ipaccess_head *hh;
92 int ret;
93
94 if (!msg)
95 return -ENOMEM;
96
97 /* first read our 3-byte header */
98 hh = (struct ipaccess_head *) msg->data;
99 ret = recv(bfd->fd, msg->data, 3, 0);
100 if (ret < 0) {
101 fprintf(stderr, "recv error %s\n", strerror(errno));
102 return ret;
103 }
104 if (ret == 0) {
105 fprintf(stderr, "BTS disappeared, dead socket\n");
Harald Welte4f361fc2009-02-15 15:32:53 +0000106 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
107 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
Harald Welte5fd8a542009-02-13 02:43:36 +0000108 bsc_unregister_fd(bfd);
109 close(bfd->fd);
110 bfd->fd = -1;
111 }
112 msgb_put(msg, ret);
113
114 /* then read te length as specified in header */
115 msg->l2h = msg->data + sizeof(*hh);
116 ret = recv(bfd->fd, msg->l2h, hh->len, 0);
117 if (ret < hh->len) {
118 fprintf(stderr, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000119 msgb_free(msg);
120 return -EIO;
Harald Welte5fd8a542009-02-13 02:43:36 +0000121 }
122 msgb_put(msg, ret);
Harald Welte5fd8a542009-02-13 02:43:36 +0000123
Harald Welte4f361fc2009-02-15 15:32:53 +0000124 if (hh->proto == IPAC_PROTO_IPACCESS)
Harald Welte5fd8a542009-02-13 02:43:36 +0000125 return ipaccess_rcvmsg(msg, bfd->fd);
126
Harald Welte3cc4bf52009-02-28 13:08:01 +0000127 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), ret));
Harald Welte5fd8a542009-02-13 02:43:36 +0000128
129 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
130 if (!link) {
131 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
132 msgb_free(msg);
133 return -EIO;
134 }
135 msg->trx = link->trx;
136
137 switch (hh->proto) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000138 case IPAC_PROTO_RSL:
Harald Welte5c1e4582009-02-15 11:57:29 +0000139 if (!rsl_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000140 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_RSL);
Harald Welte5c1e4582009-02-15 11:57:29 +0000141 rsl_up = 1;
142 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000143 ret = abis_rsl_rcvmsg(msg);
144 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000145 case IPAC_PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000146 if (!oml_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000147 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_OML);
Harald Welte7782c142009-02-15 03:39:51 +0000148 oml_up = 1;
149 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000150 ret = abis_nm_rcvmsg(msg);
151 break;
152 default:
Harald Welte7782c142009-02-15 03:39:51 +0000153 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000154 msgb_free(msg);
155 break;
156 }
157 return ret;
158}
159
160static int handle_ts1_write(struct bsc_fd *bfd)
161{
162 struct e1inp_line *line = bfd->data;
163 unsigned int ts_nr = bfd->priv_nr;
164 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
165 struct e1inp_sign_link *sign_link;
166 struct msgb *msg;
167 struct ipaccess_head *hh;
168 u_int8_t *l2_data;
169 int ret;
170
171 /* get the next msg for this timeslot */
172 msg = e1inp_tx_ts(e1i_ts, &sign_link);
173 if (!msg) {
174 bfd->when &= ~BSC_FD_WRITE;
175 return 0;
176 }
177
178 l2_data = msg->data;
179
180 /* prepend the mISDNhead */
181 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
182 hh->zero = 0;
183 hh->len = msg->len - sizeof(*hh);
184
185 switch (sign_link->type) {
186 case E1INP_SIGN_OML:
Harald Welte4f361fc2009-02-15 15:32:53 +0000187 hh->proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000188 break;
189 case E1INP_SIGN_RSL:
Harald Welte4f361fc2009-02-15 15:32:53 +0000190 hh->proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000191 break;
192 default:
193 msgb_free(msg);
194 return -EINVAL;
195 }
196
Harald Welte3cc4bf52009-02-28 13:08:01 +0000197 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(l2_data, hh->len));
Harald Welte5fd8a542009-02-13 02:43:36 +0000198
199 ret = send(bfd->fd, msg->data, msg->len, 0);
200 msgb_free(msg);
Harald Welte7782c142009-02-15 03:39:51 +0000201 usleep(100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000202
203 return ret;
204}
205
206
207/* callback from select.c in case one of the fd's can be read/written */
208static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
209{
210 struct e1inp_line *line = bfd->data;
211 unsigned int ts_nr = bfd->priv_nr;
212 unsigned int idx = ts_nr-1;
213 struct e1inp_ts *e1i_ts = &line->ts[idx];
214 int rc = 0;
215
216 switch (e1i_ts->type) {
217 case E1INP_TS_TYPE_SIGN:
218 if (what & BSC_FD_READ)
219 rc = handle_ts1_read(bfd);
220 if (what & BSC_FD_WRITE)
221 rc = handle_ts1_write(bfd);
222 break;
223#if 0
224 case E1INP_TS_TYPE_TRAU:
225 if (what & BSC_FD_READ)
226 rc = handle_tsX_read(bfd);
227 /* We never include the mISDN B-Channel FD into the
228 * writeset, since it doesn't support poll() based
229 * write flow control */
230 break;
231#endif
232 default:
233 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
234 break;
235 }
236
237 return rc;
238}
239
240
241static int ts_want_write(struct e1inp_ts *e1i_ts)
242{
243 /* We never include the mISDN B-Channel FD into the
244 * writeset, since it doesn't support poll() based
245 * write flow control */
246 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
247 return 0;
248
249 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
250
251 return 0;
252}
253
254struct e1inp_driver ipaccess_driver = {
255 .name = "ip.access",
256 .want_write = ts_want_write,
257};
258
259static int ia_e1_setup(struct e1inp_line *line)
260{
261 return 0;
262}
263
264static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
265{
266 struct e1inp_line *line = listen_bfd->data;
267 int ret;
268
269 if (what & BSC_FD_READ) {
270 int idx = 0;
271 struct e1inp_ts *e1i_ts = &line->ts[idx];
272 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
273 struct sockaddr_in sa;
274 socklen_t sa_len = sizeof(sa);
275
276 if (bfd->fd) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000277 printf("dumping old OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000278 if (bfd->fd != -1) {
279 bsc_unregister_fd(bfd);
280 close(bfd->fd);
281 }
282 }
283 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
284 if (bfd->fd < 0) {
285 perror("accept");
286 return bfd->fd;
287 }
Harald Welte5c1e4582009-02-15 11:57:29 +0000288 printf("accept()ed new OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000289 bfd->data = line;
290 bfd->priv_nr = 1;
291 bfd->cb = ipaccess_fd_cb;
292 bfd->when = BSC_FD_READ;
293 ret = bsc_register_fd(bfd);
294 if (ret < 0) {
295 fprintf(stderr, "could not register FD\n");
296 return ret;
297 }
298 }
299 return 0;
300}
301
Harald Welte5c1e4582009-02-15 11:57:29 +0000302static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
303{
304 struct e1inp_line *line = listen_bfd->data;
305 int ret;
306
307 if (what & BSC_FD_READ) {
308 int idx = 1;
309 struct e1inp_ts *e1i_ts = &line->ts[idx];
310 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
311 struct sockaddr_in sa;
312 socklen_t sa_len = sizeof(sa);
313
314 if (bfd->fd) {
315 printf("dumping old RSL fd\n");
316 if (bfd->fd != -1) {
317 bsc_unregister_fd(bfd);
318 close(bfd->fd);
319 }
320 }
321 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
322 if (bfd->fd < 0) {
323 perror("accept");
324 return bfd->fd;
325 }
326 printf("accept()ed new RSL fd\n");
327 bfd->data = line;
328 bfd->priv_nr = 2;
329 bfd->cb = ipaccess_fd_cb;
330 bfd->when = BSC_FD_READ;
331 ret = bsc_register_fd(bfd);
332 if (ret < 0) {
333 fprintf(stderr, "could not register FD\n");
334 return ret;
335 }
336 }
337 return 0;
338}
339
340static int make_sock(struct bsc_fd *bfd, u_int16_t port,
341 struct e1inp_line *line,
342 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000343{
344 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000345 int ret, on = 1;
346
347 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
348 bfd->cb = cb;
349 bfd->when = BSC_FD_READ;
350 bfd->data = line;
351
352 memset(&addr, 0, sizeof(addr));
353 addr.sin_family = AF_INET;
354 addr.sin_port = htons(port);
355 addr.sin_addr.s_addr = INADDR_ANY;
356
357 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
358
359 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
360 if (ret < 0) {
361 fprintf(stderr, "could not bind l2 socket %s\n",
362 strerror(errno));
363 return -EIO;
364 }
365
366 ret = listen(bfd->fd, 1);
367 if (ret < 0) {
368 perror("listen");
369 return ret;
370 }
371
372 ret = bsc_register_fd(bfd);
373 if (ret < 0) {
374 perror("register_listen_fd");
375 return ret;
376 }
377 return 0;
378}
379
380int ipaccess_setup(struct e1inp_line *line)
381{
Harald Welte5fd8a542009-02-13 02:43:36 +0000382 struct ia_e1_handle *e1h;
Harald Welte5c1e4582009-02-15 11:57:29 +0000383 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000384
385 /* register the driver with the core */
386 /* FIXME: do this in the plugin initializer function */
387 ret = e1inp_driver_register(&ipaccess_driver);
388 if (ret)
389 return ret;
390
391 /* create the actual line instance */
392 /* FIXME: do this independent of driver registration */
393 e1h = malloc(sizeof(*e1h));
394 memset(e1h, 0, sizeof(*e1h));
395
396 line->driver = &ipaccess_driver;
397 line->driver_data = e1h;
398
Harald Welte5c1e4582009-02-15 11:57:29 +0000399 /* Listen for OML connections */
400 ret = make_sock(&e1h->listen_fd, 3002, line, listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000401
Harald Welte5c1e4582009-02-15 11:57:29 +0000402 /* Listen for RSL connections */
403 ret = make_sock(&e1h->rsl_listen_fd, 3003, line, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000404
405 ret = ia_e1_setup(line);
406 if (ret)
407 return ret;
408
409 return e1inp_line_register(line);
410}