blob: ea7f847c2c6717f93e358fa4c1ce73ffa2de0d57 [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>
Harald Welteedb37782009-05-01 14:59:07 +000036#include <openbsc/tlv.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000037#include <openbsc/msgb.h>
38#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
40#include <openbsc/abis_nm.h>
41#include <openbsc/abis_rsl.h>
42#include <openbsc/subchan_demux.h>
43#include <openbsc/e1_input.h>
Harald Welte4f361fc2009-02-15 15:32:53 +000044#include <openbsc/ipaccess.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000045
46/* data structure for one E1 interface with A-bis */
47struct ia_e1_handle {
48 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000049 struct bsc_fd rsl_listen_fd;
Harald Welteedb37782009-05-01 14:59:07 +000050 struct gsm_network *gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +000051};
52
Harald Welteedb37782009-05-01 14:59:07 +000053static struct ia_e1_handle *e1h;
54
55
Harald Welte5fd8a542009-02-13 02:43:36 +000056#define TS1_ALLOC_SIZE 300
57
Harald Welte4f361fc2009-02-15 15:32:53 +000058static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
59static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
Harald Welteedb37782009-05-01 14:59:07 +000060static const u_int8_t id_req[] = { 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
61 0x01, IPAC_IDTAG_UNIT,
62 0x01, IPAC_IDTAG_MACADDR,
63 0x01, IPAC_IDTAG_LOCATION1,
64 0x01, IPAC_IDTAG_LOCATION2,
65 0x01, IPAC_IDTAG_EQUIPVERS,
66 0x01, IPAC_IDTAG_SWVERSION,
67 0x01, IPAC_IDTAG_UNITNAME,
68 0x01, IPAC_IDTAG_SERNR,
69 };
Harald Welte5fd8a542009-02-13 02:43:36 +000070
Harald Welteedb37782009-05-01 14:59:07 +000071static const char *idtag_names[] = {
72 [IPAC_IDTAG_SERNR] = "Serial_Number",
73 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
74 [IPAC_IDTAG_LOCATION1] = "Location_1",
75 [IPAC_IDTAG_LOCATION2] = "Location_2",
76 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
77 [IPAC_IDTAG_SWVERSION] = "Software_Version",
78 [IPAC_IDTAG_IPADDR] = "IP_Address",
79 [IPAC_IDTAG_MACADDR] = "MAC_Address",
80 [IPAC_IDTAG_UNIT] = "Unit_ID",
81};
82
83static const char *ipac_idtag_name(int tag)
Harald Welte5fd8a542009-02-13 02:43:36 +000084{
Harald Welteedb37782009-05-01 14:59:07 +000085 if (tag >= ARRAY_SIZE(idtag_names))
86 return "unknown";
87
88 return idtag_names[tag];
89}
90
91static int ipac_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
92{
93 u_int8_t t_len;
94 u_int8_t t_tag;
95 u_int8_t *cur = buf;
96
97 while (cur < buf + len) {
98 t_len = *cur++;
99 t_tag = *cur++;
100
101 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
102
103 dec->lv[t_tag].len = t_len;
104 dec->lv[t_tag].val = cur;
105
106 cur += t_len;
107 }
108 return 0;
109}
110
111struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
112 u_int16_t site_id, u_int16_t bts_id)
113{
114 int i;
115
116 for (i = 0; i < net->num_bts; i++) {
117 struct gsm_bts *bts = &net->bts[i];
118
119 if (!is_ipaccess_bts(bts))
120 continue;
121
122 if (bts->ip_access.site_id == site_id &&
123 bts->ip_access.bts_id == bts_id)
124 return bts;
125 }
126
127 return NULL;
128}
129
130static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
131 u_int16_t *trx_id)
132{
133 unsigned long ul;
134 char *endptr;
135 const char *nptr;
136
137 nptr = str;
138 ul = strtoul(nptr, &endptr, 10);
139 if (endptr <= nptr)
140 return -EINVAL;
141 if (site_id)
142 *site_id = ul & 0xffff;
143
144 if (*endptr++ != '/')
145 return -EINVAL;
146
147 nptr = endptr;
148 ul = strtoul(nptr, &endptr, 10);
149 if (endptr <= nptr)
150 return -EINVAL;
151 if (bts_id)
152 *bts_id = ul & 0xffff;
153
154 if (*endptr++ != '/')
155 return -EINVAL;
156
157 nptr = endptr;
158 ul = strtoul(nptr, &endptr, 10);
159 if (endptr <= nptr)
160 return -EINVAL;
161 if (trx_id)
162 *trx_id = ul & 0xffff;
163
164 return 0;
165}
166
167static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
168 struct bsc_fd *bfd)
169{
170 struct tlv_parsed tlvp;
Harald Welte5fd8a542009-02-13 02:43:36 +0000171 u_int8_t msg_type = *(msg->l2h);
Harald Welteedb37782009-05-01 14:59:07 +0000172 u_int16_t site_id, bts_id, trx_id;
173 struct gsm_bts *bts;
Holger Freytherff9592f2009-03-09 16:17:14 +0000174 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000175
Harald Welte5fd8a542009-02-13 02:43:36 +0000176 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000177 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000178 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000179 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000180 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000181 DEBUGP(DMI, "PONG!\n");
182 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000183 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000184 DEBUGP(DMI, "ID_RESP ");
185 /* parse tags, search for Unit ID */
186 ipac_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
187 msgb_l2len(msg)-2);
188 DEBUGP(DMI, "\n");
189
190 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
191 break;
192
193 /* lookup BTS, create sign_link, ... */
194 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
195 &site_id, &bts_id, &trx_id);
196 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
197 if (!bts) {
198 DEBUGP(DINP, "Unable to find BTS configuration for "
199 " %u/%u/%u, disconnecting\n", site_id, bts_id,
200 trx_id);
201 return -EIO;
202 }
203 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
204 if (bfd->priv_nr == 1) {
205 bts->oml_link = e1inp_sign_link_create(&line->ts[1-1],
206 E1INP_SIGN_OML, bts->c0,
207 0, 0xff);
208 } else if (bfd->priv_nr == 2) {
209 struct e1inp_ts *e1i_ts;
210 struct bsc_fd *newbfd;
211
212 /* FIXME: implement this for non-0 TRX */
213 bfd->data = line = bts->oml_link->ts->line;
214 e1i_ts = &line->ts[2-1];
215 newbfd = &e1i_ts->driver.ipaccess.fd;
216
217 bts->c0->rsl_link =
218 e1inp_sign_link_create(e1i_ts,
219 E1INP_SIGN_RSL, bts->c0,
220 0, 0);
221 /* get rid of our old temporary bfd */
222 memcpy(newbfd, bfd, sizeof(*newbfd));
223 bsc_unregister_fd(bfd);
224 bsc_register_fd(newbfd);
225 free(bfd);
226 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000227 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000228 case IPAC_MSGT_ID_ACK:
Harald Welte7782c142009-02-15 03:39:51 +0000229 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Harald Welteedb37782009-05-01 14:59:07 +0000230 ret = write(bfd->fd, id_ack, sizeof(id_ack));
Harald Welte5fd8a542009-02-13 02:43:36 +0000231 break;
232 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000233 return 0;
234}
235
Harald Welte7782c142009-02-15 03:39:51 +0000236/* FIXME: this is per BTS */
237static int oml_up = 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000238static int rsl_up = 0;
Harald Welte7782c142009-02-15 03:39:51 +0000239
Harald Welte5fd8a542009-02-13 02:43:36 +0000240static int handle_ts1_read(struct bsc_fd *bfd)
241{
242 struct e1inp_line *line = bfd->data;
243 unsigned int ts_nr = bfd->priv_nr;
244 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
245 struct e1inp_sign_link *link;
246 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
247 struct ipaccess_head *hh;
248 int ret;
249
250 if (!msg)
251 return -ENOMEM;
252
253 /* first read our 3-byte header */
254 hh = (struct ipaccess_head *) msg->data;
255 ret = recv(bfd->fd, msg->data, 3, 0);
256 if (ret < 0) {
257 fprintf(stderr, "recv error %s\n", strerror(errno));
258 return ret;
259 }
260 if (ret == 0) {
261 fprintf(stderr, "BTS disappeared, dead socket\n");
Harald Welte4f361fc2009-02-15 15:32:53 +0000262 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
263 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
Harald Welte5fd8a542009-02-13 02:43:36 +0000264 bsc_unregister_fd(bfd);
265 close(bfd->fd);
266 bfd->fd = -1;
267 }
268 msgb_put(msg, ret);
269
270 /* then read te length as specified in header */
271 msg->l2h = msg->data + sizeof(*hh);
272 ret = recv(bfd->fd, msg->l2h, hh->len, 0);
273 if (ret < hh->len) {
274 fprintf(stderr, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000275 msgb_free(msg);
276 return -EIO;
Harald Welte5fd8a542009-02-13 02:43:36 +0000277 }
278 msgb_put(msg, ret);
Harald Welte3cc4bf52009-02-28 13:08:01 +0000279 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), ret));
Harald Welte5fd8a542009-02-13 02:43:36 +0000280
Harald Welteedb37782009-05-01 14:59:07 +0000281 if (hh->proto == IPAC_PROTO_IPACCESS) {
282 ret = ipaccess_rcvmsg(line, msg, bfd);
283 if (ret < 0) {
284 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
285 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
286 bsc_unregister_fd(bfd);
287 close(bfd->fd);
288 bfd->fd = -1;
289 }
290 msgb_free(msg);
291 return ret;
292 }
293 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
294 * might have free'd it !!! */
295
Harald Welte5fd8a542009-02-13 02:43:36 +0000296 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
297 if (!link) {
298 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
299 msgb_free(msg);
300 return -EIO;
301 }
302 msg->trx = link->trx;
303
304 switch (hh->proto) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000305 case IPAC_PROTO_RSL:
Harald Welte5c1e4582009-02-15 11:57:29 +0000306 if (!rsl_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000307 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_RSL);
Harald Welte5c1e4582009-02-15 11:57:29 +0000308 rsl_up = 1;
309 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000310 ret = abis_rsl_rcvmsg(msg);
311 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000312 case IPAC_PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000313 if (!oml_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000314 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_OML);
Harald Welte7782c142009-02-15 03:39:51 +0000315 oml_up = 1;
316 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000317 ret = abis_nm_rcvmsg(msg);
318 break;
319 default:
Harald Welte7782c142009-02-15 03:39:51 +0000320 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000321 msgb_free(msg);
322 break;
323 }
324 return ret;
325}
326
327static int handle_ts1_write(struct bsc_fd *bfd)
328{
329 struct e1inp_line *line = bfd->data;
330 unsigned int ts_nr = bfd->priv_nr;
331 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
332 struct e1inp_sign_link *sign_link;
333 struct msgb *msg;
334 struct ipaccess_head *hh;
335 u_int8_t *l2_data;
336 int ret;
337
338 /* get the next msg for this timeslot */
339 msg = e1inp_tx_ts(e1i_ts, &sign_link);
340 if (!msg) {
341 bfd->when &= ~BSC_FD_WRITE;
342 return 0;
343 }
344
345 l2_data = msg->data;
346
Harald Welteedb37782009-05-01 14:59:07 +0000347 /* prepend the ip.access header */
Harald Welte5fd8a542009-02-13 02:43:36 +0000348 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
349 hh->zero = 0;
350 hh->len = msg->len - sizeof(*hh);
351
352 switch (sign_link->type) {
353 case E1INP_SIGN_OML:
Harald Welte4f361fc2009-02-15 15:32:53 +0000354 hh->proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000355 break;
356 case E1INP_SIGN_RSL:
Harald Welte4f361fc2009-02-15 15:32:53 +0000357 hh->proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000358 break;
359 default:
360 msgb_free(msg);
361 return -EINVAL;
362 }
363
Harald Welte3cc4bf52009-02-28 13:08:01 +0000364 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(l2_data, hh->len));
Harald Welte5fd8a542009-02-13 02:43:36 +0000365
366 ret = send(bfd->fd, msg->data, msg->len, 0);
367 msgb_free(msg);
Harald Welte7782c142009-02-15 03:39:51 +0000368 usleep(100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000369
370 return ret;
371}
372
Harald Welte5fd8a542009-02-13 02:43:36 +0000373/* callback from select.c in case one of the fd's can be read/written */
374static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
375{
376 struct e1inp_line *line = bfd->data;
377 unsigned int ts_nr = bfd->priv_nr;
378 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000379 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000380 int rc = 0;
381
Harald Welteedb37782009-05-01 14:59:07 +0000382 /* In case of early RSL we might not yet have a line */
383
384 if (line)
385 e1i_ts = &line->ts[idx];
386
387 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000388 if (what & BSC_FD_READ)
389 rc = handle_ts1_read(bfd);
390 if (what & BSC_FD_WRITE)
391 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000392 } else
Harald Welte5fd8a542009-02-13 02:43:36 +0000393 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000394
395 return rc;
396}
397
398
399static int ts_want_write(struct e1inp_ts *e1i_ts)
400{
Harald Welte5fd8a542009-02-13 02:43:36 +0000401 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
402
403 return 0;
404}
405
406struct e1inp_driver ipaccess_driver = {
407 .name = "ip.access",
408 .want_write = ts_want_write,
409};
410
Harald Welteedb37782009-05-01 14:59:07 +0000411/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000412static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
413{
Harald Welte5fd8a542009-02-13 02:43:36 +0000414 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000415 int idx = 0;
416 struct e1inp_line *line;
417 struct e1inp_ts *e1i_ts;
418 struct bsc_fd *bfd;
419 struct sockaddr_in sa;
420 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000421
Harald Welteedb37782009-05-01 14:59:07 +0000422 if (!(what & BSC_FD_READ))
423 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000424
Harald Welteedb37782009-05-01 14:59:07 +0000425 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
426 if (ret < 0) {
427 perror("accept");
428 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000429 }
Harald Welteedb37782009-05-01 14:59:07 +0000430 DEBUGP(DINP, "accept()ed new OML link from %s\n", inet_ntoa(sa.sin_addr));
431
432 line = malloc(sizeof(*line));
433 if (!line) {
434 close(ret);
435 return -ENOMEM;
436 }
437 memset(line, 0, sizeof(*line));
438 line->driver = &ipaccess_driver;
439 //line->driver_data = e1h;
440 /* create virrtual E1 timeslots for signalling */
441 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
442 e1inp_ts_config(&line->ts[2-1], line, E1INP_TS_TYPE_SIGN);
443
444 e1i_ts = &line->ts[idx];
445
446 bfd = &e1i_ts->driver.ipaccess.fd;
447 bfd->fd = ret;
448 bfd->data = line;
449 bfd->priv_nr = 1;
450 bfd->cb = ipaccess_fd_cb;
451 bfd->when = BSC_FD_READ;
452 ret = bsc_register_fd(bfd);
453 if (ret < 0) {
454 fprintf(stderr, "could not register FD\n");
455 close(bfd->fd);
456 free(line);
457 return ret;
458 }
459
460 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
461 ret = write(bfd->fd, id_req, sizeof(id_req));
462
463 return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000464}
465
Harald Welte5c1e4582009-02-15 11:57:29 +0000466static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
467{
Harald Welteedb37782009-05-01 14:59:07 +0000468 struct sockaddr_in sa;
469 socklen_t sa_len = sizeof(sa);
470 struct bsc_fd *bfd = malloc(sizeof(*bfd));
Harald Welte5c1e4582009-02-15 11:57:29 +0000471 int ret;
472
Harald Welteedb37782009-05-01 14:59:07 +0000473 if (!(what & BSC_FD_READ))
474 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000475
Harald Welteedb37782009-05-01 14:59:07 +0000476 /* Some BTS has connected to us, but we don't know yet which line
477 * (as created by the OML link) to associate it with. Thus, we
478 * aloocate a temporary bfd until we have received ID from BTS */
479
480 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
481 if (bfd->fd < 0) {
482 perror("accept");
483 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000484 }
Harald Welteedb37782009-05-01 14:59:07 +0000485 DEBUGP(DINP, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
486 bfd->priv_nr = 2;
487 bfd->cb = ipaccess_fd_cb;
488 bfd->when = BSC_FD_READ;
489 ret = bsc_register_fd(bfd);
490 if (ret < 0) {
491 fprintf(stderr, "could not register FD\n");
492 close(bfd->fd);
493 free(bfd);
494 return ret;
495 }
496 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
497 ret = write(bfd->fd, id_req, sizeof(id_req));
498
Harald Welte5c1e4582009-02-15 11:57:29 +0000499 return 0;
500}
501
502static int make_sock(struct bsc_fd *bfd, u_int16_t port,
Harald Welte5c1e4582009-02-15 11:57:29 +0000503 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000504{
505 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000506 int ret, on = 1;
507
508 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
509 bfd->cb = cb;
510 bfd->when = BSC_FD_READ;
Harald Welteedb37782009-05-01 14:59:07 +0000511 //bfd->data = line;
Harald Welte5c1e4582009-02-15 11:57:29 +0000512
513 memset(&addr, 0, sizeof(addr));
514 addr.sin_family = AF_INET;
515 addr.sin_port = htons(port);
516 addr.sin_addr.s_addr = INADDR_ANY;
517
518 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
519
520 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
521 if (ret < 0) {
522 fprintf(stderr, "could not bind l2 socket %s\n",
523 strerror(errno));
524 return -EIO;
525 }
526
527 ret = listen(bfd->fd, 1);
528 if (ret < 0) {
529 perror("listen");
530 return ret;
531 }
532
533 ret = bsc_register_fd(bfd);
534 if (ret < 0) {
535 perror("register_listen_fd");
536 return ret;
537 }
538 return 0;
539}
540
Harald Welte25de9912009-04-30 15:53:07 +0000541/* Actively connect to a BTS. Currently used by ipaccess-config.c */
542int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
543{
544 struct e1inp_ts *e1i_ts = &line->ts[0];
545 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
546 int ret, on = 1;
547
548 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
549 bfd->cb = ipaccess_fd_cb;
550 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
551 bfd->data = line;
552 bfd->priv_nr = 1;
553
554 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
555
556 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
557 if (ret < 0) {
558 fprintf(stderr, "could not connect socket\n");
559 close(bfd->fd);
560 return ret;
561 }
562
563 ret = bsc_register_fd(bfd);
564 if (ret < 0) {
565 close(bfd->fd);
566 return ret;
567 }
568
569 line->driver = &ipaccess_driver;
570
571 return e1inp_line_register(line);
572}
573
Harald Welteedb37782009-05-01 14:59:07 +0000574int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000575{
Harald Welte5c1e4582009-02-15 11:57:29 +0000576 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000577
578 /* register the driver with the core */
579 /* FIXME: do this in the plugin initializer function */
580 ret = e1inp_driver_register(&ipaccess_driver);
581 if (ret)
582 return ret;
583
Harald Welte5fd8a542009-02-13 02:43:36 +0000584 e1h = malloc(sizeof(*e1h));
585 memset(e1h, 0, sizeof(*e1h));
Harald Welteedb37782009-05-01 14:59:07 +0000586 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000587
Harald Welte5c1e4582009-02-15 11:57:29 +0000588 /* Listen for OML connections */
Harald Welteedb37782009-05-01 14:59:07 +0000589 ret = make_sock(&e1h->listen_fd, 3002, listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000590 if (ret < 0)
591 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000592
Harald Welte5c1e4582009-02-15 11:57:29 +0000593 /* Listen for RSL connections */
Harald Welteedb37782009-05-01 14:59:07 +0000594 ret = make_sock(&e1h->rsl_listen_fd, 3003, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000595
Harald Welteedb37782009-05-01 14:59:07 +0000596 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000597}