blob: ad32259f904a5ca5785fc18a399496e08d4ace9b [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 Welte5fd8a542009-02-13 02:43:36 +0000368
369 return ret;
370}
371
Harald Welte5fd8a542009-02-13 02:43:36 +0000372/* callback from select.c in case one of the fd's can be read/written */
373static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
374{
375 struct e1inp_line *line = bfd->data;
376 unsigned int ts_nr = bfd->priv_nr;
377 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000378 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000379 int rc = 0;
380
Harald Welteedb37782009-05-01 14:59:07 +0000381 /* In case of early RSL we might not yet have a line */
382
383 if (line)
384 e1i_ts = &line->ts[idx];
385
386 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000387 if (what & BSC_FD_READ)
388 rc = handle_ts1_read(bfd);
389 if (what & BSC_FD_WRITE)
390 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000391 } else
Harald Welte5fd8a542009-02-13 02:43:36 +0000392 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000393
394 return rc;
395}
396
397
398static int ts_want_write(struct e1inp_ts *e1i_ts)
399{
Harald Welte5fd8a542009-02-13 02:43:36 +0000400 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
401
402 return 0;
403}
404
405struct e1inp_driver ipaccess_driver = {
406 .name = "ip.access",
407 .want_write = ts_want_write,
408};
409
Harald Welteedb37782009-05-01 14:59:07 +0000410/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000411static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
412{
Harald Welte5fd8a542009-02-13 02:43:36 +0000413 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000414 int idx = 0;
415 struct e1inp_line *line;
416 struct e1inp_ts *e1i_ts;
417 struct bsc_fd *bfd;
418 struct sockaddr_in sa;
419 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000420
Harald Welteedb37782009-05-01 14:59:07 +0000421 if (!(what & BSC_FD_READ))
422 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000423
Harald Welteedb37782009-05-01 14:59:07 +0000424 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
425 if (ret < 0) {
426 perror("accept");
427 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000428 }
Harald Welteedb37782009-05-01 14:59:07 +0000429 DEBUGP(DINP, "accept()ed new OML link from %s\n", inet_ntoa(sa.sin_addr));
430
431 line = malloc(sizeof(*line));
432 if (!line) {
433 close(ret);
434 return -ENOMEM;
435 }
436 memset(line, 0, sizeof(*line));
437 line->driver = &ipaccess_driver;
438 //line->driver_data = e1h;
439 /* create virrtual E1 timeslots for signalling */
440 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
441 e1inp_ts_config(&line->ts[2-1], line, E1INP_TS_TYPE_SIGN);
442
443 e1i_ts = &line->ts[idx];
444
445 bfd = &e1i_ts->driver.ipaccess.fd;
446 bfd->fd = ret;
447 bfd->data = line;
448 bfd->priv_nr = 1;
449 bfd->cb = ipaccess_fd_cb;
450 bfd->when = BSC_FD_READ;
451 ret = bsc_register_fd(bfd);
452 if (ret < 0) {
453 fprintf(stderr, "could not register FD\n");
454 close(bfd->fd);
455 free(line);
456 return ret;
457 }
458
459 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
460 ret = write(bfd->fd, id_req, sizeof(id_req));
461
462 return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000463}
464
Harald Welte5c1e4582009-02-15 11:57:29 +0000465static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
466{
Harald Welteedb37782009-05-01 14:59:07 +0000467 struct sockaddr_in sa;
468 socklen_t sa_len = sizeof(sa);
469 struct bsc_fd *bfd = malloc(sizeof(*bfd));
Harald Welte5c1e4582009-02-15 11:57:29 +0000470 int ret;
471
Harald Welteedb37782009-05-01 14:59:07 +0000472 if (!(what & BSC_FD_READ))
473 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000474
Harald Welteedb37782009-05-01 14:59:07 +0000475 /* Some BTS has connected to us, but we don't know yet which line
476 * (as created by the OML link) to associate it with. Thus, we
477 * aloocate a temporary bfd until we have received ID from BTS */
478
479 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
480 if (bfd->fd < 0) {
481 perror("accept");
482 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000483 }
Harald Welteedb37782009-05-01 14:59:07 +0000484 DEBUGP(DINP, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
485 bfd->priv_nr = 2;
486 bfd->cb = ipaccess_fd_cb;
487 bfd->when = BSC_FD_READ;
488 ret = bsc_register_fd(bfd);
489 if (ret < 0) {
490 fprintf(stderr, "could not register FD\n");
491 close(bfd->fd);
492 free(bfd);
493 return ret;
494 }
495 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
496 ret = write(bfd->fd, id_req, sizeof(id_req));
497
Harald Welte5c1e4582009-02-15 11:57:29 +0000498 return 0;
499}
500
501static int make_sock(struct bsc_fd *bfd, u_int16_t port,
Harald Welte5c1e4582009-02-15 11:57:29 +0000502 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000503{
504 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000505 int ret, on = 1;
506
507 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
508 bfd->cb = cb;
509 bfd->when = BSC_FD_READ;
Harald Welteedb37782009-05-01 14:59:07 +0000510 //bfd->data = line;
Harald Welte5c1e4582009-02-15 11:57:29 +0000511
512 memset(&addr, 0, sizeof(addr));
513 addr.sin_family = AF_INET;
514 addr.sin_port = htons(port);
515 addr.sin_addr.s_addr = INADDR_ANY;
516
517 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
518
519 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
520 if (ret < 0) {
521 fprintf(stderr, "could not bind l2 socket %s\n",
522 strerror(errno));
523 return -EIO;
524 }
525
526 ret = listen(bfd->fd, 1);
527 if (ret < 0) {
528 perror("listen");
529 return ret;
530 }
531
532 ret = bsc_register_fd(bfd);
533 if (ret < 0) {
534 perror("register_listen_fd");
535 return ret;
536 }
537 return 0;
538}
539
Harald Welte25de9912009-04-30 15:53:07 +0000540/* Actively connect to a BTS. Currently used by ipaccess-config.c */
541int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
542{
543 struct e1inp_ts *e1i_ts = &line->ts[0];
544 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
545 int ret, on = 1;
546
547 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
548 bfd->cb = ipaccess_fd_cb;
549 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
550 bfd->data = line;
551 bfd->priv_nr = 1;
552
553 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
554
555 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
556 if (ret < 0) {
557 fprintf(stderr, "could not connect socket\n");
558 close(bfd->fd);
559 return ret;
560 }
561
562 ret = bsc_register_fd(bfd);
563 if (ret < 0) {
564 close(bfd->fd);
565 return ret;
566 }
567
568 line->driver = &ipaccess_driver;
569
570 return e1inp_line_register(line);
571}
572
Harald Welteedb37782009-05-01 14:59:07 +0000573int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000574{
Harald Welte5c1e4582009-02-15 11:57:29 +0000575 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000576
577 /* register the driver with the core */
578 /* FIXME: do this in the plugin initializer function */
579 ret = e1inp_driver_register(&ipaccess_driver);
580 if (ret)
581 return ret;
582
Harald Welte5fd8a542009-02-13 02:43:36 +0000583 e1h = malloc(sizeof(*e1h));
584 memset(e1h, 0, sizeof(*e1h));
Harald Welteedb37782009-05-01 14:59:07 +0000585 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000586
Harald Welte5c1e4582009-02-15 11:57:29 +0000587 /* Listen for OML connections */
Harald Welteedb37782009-05-01 14:59:07 +0000588 ret = make_sock(&e1h->listen_fd, 3002, listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000589 if (ret < 0)
590 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000591
Harald Welte5c1e4582009-02-15 11:57:29 +0000592 /* Listen for RSL connections */
Harald Welteedb37782009-05-01 14:59:07 +0000593 ret = make_sock(&e1h->rsl_listen_fd, 3003, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000594
Harald Welteedb37782009-05-01 14:59:07 +0000595 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000596}