blob: 98f47fb6c5fe43f2293c0636abd89c09f725bba7 [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
127 if (debug_mask & DMI) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000128 fprintf(stdout, "RX %u: ", ts_nr);
Harald Welte5fd8a542009-02-13 02:43:36 +0000129 hexdump(msgb_l2(msg), ret);
130 }
131
132 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
133 if (!link) {
134 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
135 msgb_free(msg);
136 return -EIO;
137 }
138 msg->trx = link->trx;
139
140 switch (hh->proto) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000141 case IPAC_PROTO_RSL:
Harald Welte5c1e4582009-02-15 11:57:29 +0000142 if (!rsl_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000143 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_RSL);
Harald Welte5c1e4582009-02-15 11:57:29 +0000144 rsl_up = 1;
145 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000146 ret = abis_rsl_rcvmsg(msg);
147 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000148 case IPAC_PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000149 if (!oml_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000150 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_OML);
Harald Welte7782c142009-02-15 03:39:51 +0000151 oml_up = 1;
152 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000153 ret = abis_nm_rcvmsg(msg);
154 break;
155 default:
Harald Welte7782c142009-02-15 03:39:51 +0000156 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000157 msgb_free(msg);
158 break;
159 }
160 return ret;
161}
162
163static int handle_ts1_write(struct bsc_fd *bfd)
164{
165 struct e1inp_line *line = bfd->data;
166 unsigned int ts_nr = bfd->priv_nr;
167 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
168 struct e1inp_sign_link *sign_link;
169 struct msgb *msg;
170 struct ipaccess_head *hh;
171 u_int8_t *l2_data;
172 int ret;
173
174 /* get the next msg for this timeslot */
175 msg = e1inp_tx_ts(e1i_ts, &sign_link);
176 if (!msg) {
177 bfd->when &= ~BSC_FD_WRITE;
178 return 0;
179 }
180
181 l2_data = msg->data;
182
183 /* prepend the mISDNhead */
184 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
185 hh->zero = 0;
186 hh->len = msg->len - sizeof(*hh);
187
188 switch (sign_link->type) {
189 case E1INP_SIGN_OML:
Harald Welte4f361fc2009-02-15 15:32:53 +0000190 hh->proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000191 break;
192 case E1INP_SIGN_RSL:
Harald Welte4f361fc2009-02-15 15:32:53 +0000193 hh->proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000194 break;
195 default:
196 msgb_free(msg);
197 return -EINVAL;
198 }
199
200 if (debug_mask & DMI) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000201 fprintf(stdout, "TX %u: ", ts_nr);
Harald Welte5fd8a542009-02-13 02:43:36 +0000202 hexdump(l2_data, hh->len);
203 }
204
205 ret = send(bfd->fd, msg->data, msg->len, 0);
206 msgb_free(msg);
Harald Welte7782c142009-02-15 03:39:51 +0000207 usleep(100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000208
209 return ret;
210}
211
212
213/* callback from select.c in case one of the fd's can be read/written */
214static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
215{
216 struct e1inp_line *line = bfd->data;
217 unsigned int ts_nr = bfd->priv_nr;
218 unsigned int idx = ts_nr-1;
219 struct e1inp_ts *e1i_ts = &line->ts[idx];
220 int rc = 0;
221
222 switch (e1i_ts->type) {
223 case E1INP_TS_TYPE_SIGN:
224 if (what & BSC_FD_READ)
225 rc = handle_ts1_read(bfd);
226 if (what & BSC_FD_WRITE)
227 rc = handle_ts1_write(bfd);
228 break;
229#if 0
230 case E1INP_TS_TYPE_TRAU:
231 if (what & BSC_FD_READ)
232 rc = handle_tsX_read(bfd);
233 /* We never include the mISDN B-Channel FD into the
234 * writeset, since it doesn't support poll() based
235 * write flow control */
236 break;
237#endif
238 default:
239 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
240 break;
241 }
242
243 return rc;
244}
245
246
247static int ts_want_write(struct e1inp_ts *e1i_ts)
248{
249 /* We never include the mISDN B-Channel FD into the
250 * writeset, since it doesn't support poll() based
251 * write flow control */
252 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
253 return 0;
254
255 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
256
257 return 0;
258}
259
260struct e1inp_driver ipaccess_driver = {
261 .name = "ip.access",
262 .want_write = ts_want_write,
263};
264
265static int ia_e1_setup(struct e1inp_line *line)
266{
267 return 0;
268}
269
270static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
271{
272 struct e1inp_line *line = listen_bfd->data;
273 int ret;
274
275 if (what & BSC_FD_READ) {
276 int idx = 0;
277 struct e1inp_ts *e1i_ts = &line->ts[idx];
278 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
279 struct sockaddr_in sa;
280 socklen_t sa_len = sizeof(sa);
281
282 if (bfd->fd) {
Harald Welte5c1e4582009-02-15 11:57:29 +0000283 printf("dumping old OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000284 if (bfd->fd != -1) {
285 bsc_unregister_fd(bfd);
286 close(bfd->fd);
287 }
288 }
289 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
290 if (bfd->fd < 0) {
291 perror("accept");
292 return bfd->fd;
293 }
Harald Welte5c1e4582009-02-15 11:57:29 +0000294 printf("accept()ed new OML fd\n");
Harald Welte5fd8a542009-02-13 02:43:36 +0000295 bfd->data = line;
296 bfd->priv_nr = 1;
297 bfd->cb = ipaccess_fd_cb;
298 bfd->when = BSC_FD_READ;
299 ret = bsc_register_fd(bfd);
300 if (ret < 0) {
301 fprintf(stderr, "could not register FD\n");
302 return ret;
303 }
304 }
305 return 0;
306}
307
Harald Welte5c1e4582009-02-15 11:57:29 +0000308static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
309{
310 struct e1inp_line *line = listen_bfd->data;
311 int ret;
312
313 if (what & BSC_FD_READ) {
314 int idx = 1;
315 struct e1inp_ts *e1i_ts = &line->ts[idx];
316 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
317 struct sockaddr_in sa;
318 socklen_t sa_len = sizeof(sa);
319
320 if (bfd->fd) {
321 printf("dumping old RSL fd\n");
322 if (bfd->fd != -1) {
323 bsc_unregister_fd(bfd);
324 close(bfd->fd);
325 }
326 }
327 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
328 if (bfd->fd < 0) {
329 perror("accept");
330 return bfd->fd;
331 }
332 printf("accept()ed new RSL fd\n");
333 bfd->data = line;
334 bfd->priv_nr = 2;
335 bfd->cb = ipaccess_fd_cb;
336 bfd->when = BSC_FD_READ;
337 ret = bsc_register_fd(bfd);
338 if (ret < 0) {
339 fprintf(stderr, "could not register FD\n");
340 return ret;
341 }
342 }
343 return 0;
344}
345
346static int make_sock(struct bsc_fd *bfd, u_int16_t port,
347 struct e1inp_line *line,
348 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000349{
350 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000351 int ret, on = 1;
352
353 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
354 bfd->cb = cb;
355 bfd->when = BSC_FD_READ;
356 bfd->data = line;
357
358 memset(&addr, 0, sizeof(addr));
359 addr.sin_family = AF_INET;
360 addr.sin_port = htons(port);
361 addr.sin_addr.s_addr = INADDR_ANY;
362
363 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
364
365 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
366 if (ret < 0) {
367 fprintf(stderr, "could not bind l2 socket %s\n",
368 strerror(errno));
369 return -EIO;
370 }
371
372 ret = listen(bfd->fd, 1);
373 if (ret < 0) {
374 perror("listen");
375 return ret;
376 }
377
378 ret = bsc_register_fd(bfd);
379 if (ret < 0) {
380 perror("register_listen_fd");
381 return ret;
382 }
383 return 0;
384}
385
386int ipaccess_setup(struct e1inp_line *line)
387{
Harald Welte5fd8a542009-02-13 02:43:36 +0000388 struct ia_e1_handle *e1h;
Harald Welte5c1e4582009-02-15 11:57:29 +0000389 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000390
391 /* register the driver with the core */
392 /* FIXME: do this in the plugin initializer function */
393 ret = e1inp_driver_register(&ipaccess_driver);
394 if (ret)
395 return ret;
396
397 /* create the actual line instance */
398 /* FIXME: do this independent of driver registration */
399 e1h = malloc(sizeof(*e1h));
400 memset(e1h, 0, sizeof(*e1h));
401
402 line->driver = &ipaccess_driver;
403 line->driver_data = e1h;
404
Harald Welte5c1e4582009-02-15 11:57:29 +0000405 /* Listen for OML connections */
406 ret = make_sock(&e1h->listen_fd, 3002, line, listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000407
Harald Welte5c1e4582009-02-15 11:57:29 +0000408 /* Listen for RSL connections */
409 ret = make_sock(&e1h->rsl_listen_fd, 3003, line, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000410
411 ret = ia_e1_setup(line);
412 if (ret)
413 return ret;
414
415 return e1inp_line_register(line);
416}