blob: 22582678f05b5565be1f248bbd63ebbcb4d70a04 [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;
47};
48
49#define TS1_ALLOC_SIZE 300
50
51struct ipaccess_head {
52 u_int8_t zero;
53 u_int8_t len;
54 u_int8_t proto;
55 u_int8_t data[0];
56} __attribute__ ((packed));
57
58enum ipaccess_proto {
59 PROTO_RSL = 0x00,
60 PROTO_IPACCESS = 0xfe,
61 PROTO_OML = 0xff,
62};
63
64enum ipaccess_msg_type {
65 MSGT_PING = 0x00,
66 MSGT_PONG = 0x01,
67 MSGT_IDENTITY_GET = 0x04,
68 MSGT_IDENTITY_RESP = 0x05,
69 MSGT_IDENTITY_ACK = 0x06,
70};
71
72static const u_int8_t pong[] = { 0, 1, PROTO_IPACCESS, MSGT_PONG };
73static const u_int8_t id_ack[] = { 0, 1, PROTO_IPACCESS, MSGT_IDENTITY_ACK };
74
75static int ipaccess_rcvmsg(struct msgb *msg, int fd)
76{
77 u_int8_t msg_type = *(msg->l2h);
78
Harald Welte5fd8a542009-02-13 02:43:36 +000079 switch (msg_type) {
80 case MSGT_PING:
Harald Welte5fd8a542009-02-13 02:43:36 +000081 write(fd, pong, sizeof(pong));
82 break;
83 case MSGT_PONG:
84 DEBUGP(DMI, "PONG!\n");
85 break;
86 case MSGT_IDENTITY_RESP:
87 DEBUGP(DMI, "ID_RESP\n");
88 break;
89 case MSGT_IDENTITY_ACK:
Harald Welte7782c142009-02-15 03:39:51 +000090 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Harald Welte5fd8a542009-02-13 02:43:36 +000091 write(fd, id_ack, sizeof(id_ack));
92 break;
93 }
94
95 msgb_free(msg);
96 return 0;
97}
98
Harald Welte7782c142009-02-15 03:39:51 +000099/* FIXME: this is per BTS */
100static int oml_up = 0;
101
Harald Welte5fd8a542009-02-13 02:43:36 +0000102static int handle_ts1_read(struct bsc_fd *bfd)
103{
104 struct e1inp_line *line = bfd->data;
105 unsigned int ts_nr = bfd->priv_nr;
106 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
107 struct e1inp_sign_link *link;
108 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
109 struct ipaccess_head *hh;
110 int ret;
111
112 if (!msg)
113 return -ENOMEM;
114
115 /* first read our 3-byte header */
116 hh = (struct ipaccess_head *) msg->data;
117 ret = recv(bfd->fd, msg->data, 3, 0);
118 if (ret < 0) {
119 fprintf(stderr, "recv error %s\n", strerror(errno));
120 return ret;
121 }
122 if (ret == 0) {
123 fprintf(stderr, "BTS disappeared, dead socket\n");
Harald Welte7782c142009-02-15 03:39:51 +0000124 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, PROTO_RSL);
125 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, PROTO_OML);
Harald Welte5fd8a542009-02-13 02:43:36 +0000126 bsc_unregister_fd(bfd);
127 close(bfd->fd);
128 bfd->fd = -1;
129 }
130 msgb_put(msg, ret);
131
132 /* then read te length as specified in header */
133 msg->l2h = msg->data + sizeof(*hh);
134 ret = recv(bfd->fd, msg->l2h, hh->len, 0);
135 if (ret < hh->len) {
136 fprintf(stderr, "short read!\n");
137 //msgb_free(msg);
138 //return -EIO;
139 }
140 msgb_put(msg, ret);
Harald Welte5fd8a542009-02-13 02:43:36 +0000141
142 if (hh->proto == PROTO_IPACCESS)
143 return ipaccess_rcvmsg(msg, bfd->fd);
144
145 if (debug_mask & DMI) {
146 fprintf(stdout, "RX: ");
147 hexdump(msgb_l2(msg), ret);
148 }
149
150 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
151 if (!link) {
152 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
153 msgb_free(msg);
154 return -EIO;
155 }
156 msg->trx = link->trx;
157
158 switch (hh->proto) {
159 case PROTO_RSL:
160 ret = abis_rsl_rcvmsg(msg);
161 break;
162 case PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000163 if (!oml_up) {
164 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, PROTO_OML);
165 oml_up = 1;
166 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000167 ret = abis_nm_rcvmsg(msg);
168 break;
169 default:
Harald Welte7782c142009-02-15 03:39:51 +0000170 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000171 msgb_free(msg);
172 break;
173 }
174 return ret;
175}
176
177static int handle_ts1_write(struct bsc_fd *bfd)
178{
179 struct e1inp_line *line = bfd->data;
180 unsigned int ts_nr = bfd->priv_nr;
181 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
182 struct e1inp_sign_link *sign_link;
183 struct msgb *msg;
184 struct ipaccess_head *hh;
185 u_int8_t *l2_data;
186 int ret;
187
188 /* get the next msg for this timeslot */
189 msg = e1inp_tx_ts(e1i_ts, &sign_link);
190 if (!msg) {
191 bfd->when &= ~BSC_FD_WRITE;
192 return 0;
193 }
194
195 l2_data = msg->data;
196
197 /* prepend the mISDNhead */
198 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
199 hh->zero = 0;
200 hh->len = msg->len - sizeof(*hh);
201
202 switch (sign_link->type) {
203 case E1INP_SIGN_OML:
204 hh->proto = PROTO_OML;
205 break;
206 case E1INP_SIGN_RSL:
207 hh->proto = PROTO_RSL;
208 break;
209 default:
210 msgb_free(msg);
211 return -EINVAL;
212 }
213
214 if (debug_mask & DMI) {
Harald Welte7782c142009-02-15 03:39:51 +0000215 fprintf(stdout, "TX: ");
Harald Welte5fd8a542009-02-13 02:43:36 +0000216 hexdump(l2_data, hh->len);
217 }
218
219 ret = send(bfd->fd, msg->data, msg->len, 0);
220 msgb_free(msg);
Harald Welte7782c142009-02-15 03:39:51 +0000221 usleep(100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000222
223 return ret;
224}
225
226
227/* callback from select.c in case one of the fd's can be read/written */
228static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
229{
230 struct e1inp_line *line = bfd->data;
231 unsigned int ts_nr = bfd->priv_nr;
232 unsigned int idx = ts_nr-1;
233 struct e1inp_ts *e1i_ts = &line->ts[idx];
234 int rc = 0;
235
236 switch (e1i_ts->type) {
237 case E1INP_TS_TYPE_SIGN:
238 if (what & BSC_FD_READ)
239 rc = handle_ts1_read(bfd);
240 if (what & BSC_FD_WRITE)
241 rc = handle_ts1_write(bfd);
242 break;
243#if 0
244 case E1INP_TS_TYPE_TRAU:
245 if (what & BSC_FD_READ)
246 rc = handle_tsX_read(bfd);
247 /* We never include the mISDN B-Channel FD into the
248 * writeset, since it doesn't support poll() based
249 * write flow control */
250 break;
251#endif
252 default:
253 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
254 break;
255 }
256
257 return rc;
258}
259
260
261static int ts_want_write(struct e1inp_ts *e1i_ts)
262{
263 /* We never include the mISDN B-Channel FD into the
264 * writeset, since it doesn't support poll() based
265 * write flow control */
266 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
267 return 0;
268
269 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
270
271 return 0;
272}
273
274struct e1inp_driver ipaccess_driver = {
275 .name = "ip.access",
276 .want_write = ts_want_write,
277};
278
279static int ia_e1_setup(struct e1inp_line *line)
280{
281 return 0;
282}
283
284static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
285{
286 struct e1inp_line *line = listen_bfd->data;
287 int ret;
288
289 if (what & BSC_FD_READ) {
290 int idx = 0;
291 struct e1inp_ts *e1i_ts = &line->ts[idx];
292 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
293 struct sockaddr_in sa;
294 socklen_t sa_len = sizeof(sa);
295
296 if (bfd->fd) {
297 printf("dumping old fd\n");
298 if (bfd->fd != -1) {
299 bsc_unregister_fd(bfd);
300 close(bfd->fd);
301 }
302 }
303 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
304 if (bfd->fd < 0) {
305 perror("accept");
306 return bfd->fd;
307 }
308 printf("accept()ed new RSL/OML fd\n");
309 bfd->data = line;
310 bfd->priv_nr = 1;
311 bfd->cb = ipaccess_fd_cb;
312 bfd->when = BSC_FD_READ;
313 ret = bsc_register_fd(bfd);
314 if (ret < 0) {
315 fprintf(stderr, "could not register FD\n");
316 return ret;
317 }
318 }
319 return 0;
320}
321
322int ipaccess_setup(struct e1inp_line *line)
323{
324 struct sockaddr_in addr;
325 struct ia_e1_handle *e1h;
326 int sk, ret, on = 1;
327
328 /* register the driver with the core */
329 /* FIXME: do this in the plugin initializer function */
330 ret = e1inp_driver_register(&ipaccess_driver);
331 if (ret)
332 return ret;
333
334 /* create the actual line instance */
335 /* FIXME: do this independent of driver registration */
336 e1h = malloc(sizeof(*e1h));
337 memset(e1h, 0, sizeof(*e1h));
338
339 line->driver = &ipaccess_driver;
340 line->driver_data = e1h;
341
342 e1h->listen_fd.fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
343 e1h->listen_fd.cb = listen_fd_cb;
344 e1h->listen_fd.when = BSC_FD_READ;
345 e1h->listen_fd.data = line;
346
347 memset(&addr, 0, sizeof(addr));
348 addr.sin_family = AF_INET;
349 addr.sin_port = htons(3002);
350 addr.sin_addr.s_addr = INADDR_ANY;
351
352 setsockopt(e1h->listen_fd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
353
354 ret = bind(e1h->listen_fd.fd, (struct sockaddr *) &addr, sizeof(addr));
355 if (ret < 0) {
356 fprintf(stderr, "could not bind l2 socket %s\n",
357 strerror(errno));
358 return -EIO;
359 }
360
361 ret = listen(e1h->listen_fd.fd, 1);
362 if (ret < 0) {
363 perror("listen");
364 return ret;
365 }
366
367 ret = bsc_register_fd(&e1h->listen_fd);
368 if (ret < 0) {
369 perror("register_listen_fd");
370 return ret;
371 }
372
373 ret = ia_e1_setup(line);
374 if (ret)
375 return ret;
376
377 return e1inp_line_register(line);
378}