blob: 7d7e33ca4ddce4347db6ba7245939f817441f30d [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 Welte2cf161b2009-06-20 22:36:41 +020045#include <openbsc/talloc.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000046
47/* data structure for one E1 interface with A-bis */
48struct ia_e1_handle {
49 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000050 struct bsc_fd rsl_listen_fd;
Harald Welteedb37782009-05-01 14:59:07 +000051 struct gsm_network *gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +000052};
53
Harald Welteedb37782009-05-01 14:59:07 +000054static struct ia_e1_handle *e1h;
55
56
Harald Welte5fd8a542009-02-13 02:43:36 +000057#define TS1_ALLOC_SIZE 300
58
Harald Welte4f361fc2009-02-15 15:32:53 +000059static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
60static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
Harald Welteedb37782009-05-01 14:59:07 +000061static const u_int8_t id_req[] = { 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
62 0x01, IPAC_IDTAG_UNIT,
63 0x01, IPAC_IDTAG_MACADDR,
64 0x01, IPAC_IDTAG_LOCATION1,
65 0x01, IPAC_IDTAG_LOCATION2,
66 0x01, IPAC_IDTAG_EQUIPVERS,
67 0x01, IPAC_IDTAG_SWVERSION,
68 0x01, IPAC_IDTAG_UNITNAME,
69 0x01, IPAC_IDTAG_SERNR,
70 };
Harald Welte5fd8a542009-02-13 02:43:36 +000071
Harald Welteedb37782009-05-01 14:59:07 +000072static const char *idtag_names[] = {
73 [IPAC_IDTAG_SERNR] = "Serial_Number",
74 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
75 [IPAC_IDTAG_LOCATION1] = "Location_1",
76 [IPAC_IDTAG_LOCATION2] = "Location_2",
77 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
78 [IPAC_IDTAG_SWVERSION] = "Software_Version",
79 [IPAC_IDTAG_IPADDR] = "IP_Address",
80 [IPAC_IDTAG_MACADDR] = "MAC_Address",
81 [IPAC_IDTAG_UNIT] = "Unit_ID",
82};
83
84static const char *ipac_idtag_name(int tag)
Harald Welte5fd8a542009-02-13 02:43:36 +000085{
Harald Welteedb37782009-05-01 14:59:07 +000086 if (tag >= ARRAY_SIZE(idtag_names))
87 return "unknown";
88
89 return idtag_names[tag];
90}
91
92static int ipac_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
93{
94 u_int8_t t_len;
95 u_int8_t t_tag;
96 u_int8_t *cur = buf;
97
98 while (cur < buf + len) {
99 t_len = *cur++;
100 t_tag = *cur++;
101
102 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
103
104 dec->lv[t_tag].len = t_len;
105 dec->lv[t_tag].val = cur;
106
107 cur += t_len;
108 }
109 return 0;
110}
111
112struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
113 u_int16_t site_id, u_int16_t bts_id)
114{
Harald Weltee441d9c2009-06-21 16:17:15 +0200115 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000116
Harald Weltee441d9c2009-06-21 16:17:15 +0200117 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000118
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);
Holger Hans Peter Freyther336f5092009-08-10 08:01:12 +0200172 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
Harald Welteedb37782009-05-01 14:59:07 +0000173 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);
Harald Weltea4ffea92009-06-22 01:36:25 +0200225 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000226 }
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;
Harald Welte966636f2009-06-26 19:39:35 +0200246 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000247 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 Welte7a2a71e2009-05-01 21:54:11 +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
Harald Welte2cf161b2009-06-20 22:36:41 +0200432 line = talloc(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000433 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);
Harald Weltea4ffea92009-06-22 01:36:25 +0200456 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000457 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
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200463 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200464 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000465}
466
Harald Welte5c1e4582009-02-15 11:57:29 +0000467static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
468{
Harald Welteedb37782009-05-01 14:59:07 +0000469 struct sockaddr_in sa;
470 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200471 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000472 int ret;
473
Harald Welteedb37782009-05-01 14:59:07 +0000474 if (!(what & BSC_FD_READ))
475 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000476
Harald Weltea4ffea92009-06-22 01:36:25 +0200477 bfd = talloc(tall_bsc_ctx, struct bsc_fd);
478 if (!bfd)
479 return -ENOMEM;
480 memset(bfd, 0, sizeof(*bfd));
481
Harald Welteedb37782009-05-01 14:59:07 +0000482 /* Some BTS has connected to us, but we don't know yet which line
483 * (as created by the OML link) to associate it with. Thus, we
484 * aloocate a temporary bfd until we have received ID from BTS */
485
486 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
487 if (bfd->fd < 0) {
488 perror("accept");
489 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000490 }
Harald Welteedb37782009-05-01 14:59:07 +0000491 DEBUGP(DINP, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
492 bfd->priv_nr = 2;
493 bfd->cb = ipaccess_fd_cb;
494 bfd->when = BSC_FD_READ;
495 ret = bsc_register_fd(bfd);
496 if (ret < 0) {
497 fprintf(stderr, "could not register FD\n");
498 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200499 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000500 return ret;
501 }
502 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
503 ret = write(bfd->fd, id_req, sizeof(id_req));
504
Harald Welte5c1e4582009-02-15 11:57:29 +0000505 return 0;
506}
507
508static int make_sock(struct bsc_fd *bfd, u_int16_t port,
Harald Welte5c1e4582009-02-15 11:57:29 +0000509 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000510{
511 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000512 int ret, on = 1;
513
514 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
515 bfd->cb = cb;
516 bfd->when = BSC_FD_READ;
Harald Welteedb37782009-05-01 14:59:07 +0000517 //bfd->data = line;
Harald Welte5c1e4582009-02-15 11:57:29 +0000518
519 memset(&addr, 0, sizeof(addr));
520 addr.sin_family = AF_INET;
521 addr.sin_port = htons(port);
522 addr.sin_addr.s_addr = INADDR_ANY;
523
524 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
525
526 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
527 if (ret < 0) {
528 fprintf(stderr, "could not bind l2 socket %s\n",
529 strerror(errno));
530 return -EIO;
531 }
532
533 ret = listen(bfd->fd, 1);
534 if (ret < 0) {
535 perror("listen");
536 return ret;
537 }
538
539 ret = bsc_register_fd(bfd);
540 if (ret < 0) {
541 perror("register_listen_fd");
542 return ret;
543 }
544 return 0;
545}
546
Harald Welte25de9912009-04-30 15:53:07 +0000547/* Actively connect to a BTS. Currently used by ipaccess-config.c */
548int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
549{
550 struct e1inp_ts *e1i_ts = &line->ts[0];
551 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
552 int ret, on = 1;
553
554 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
555 bfd->cb = ipaccess_fd_cb;
556 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
557 bfd->data = line;
558 bfd->priv_nr = 1;
559
560 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
561
562 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
563 if (ret < 0) {
564 fprintf(stderr, "could not connect socket\n");
565 close(bfd->fd);
566 return ret;
567 }
568
569 ret = bsc_register_fd(bfd);
570 if (ret < 0) {
571 close(bfd->fd);
572 return ret;
573 }
574
575 line->driver = &ipaccess_driver;
576
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200577 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200578 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000579}
580
Harald Welteedb37782009-05-01 14:59:07 +0000581int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000582{
Harald Welte5c1e4582009-02-15 11:57:29 +0000583 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000584
585 /* register the driver with the core */
586 /* FIXME: do this in the plugin initializer function */
587 ret = e1inp_driver_register(&ipaccess_driver);
588 if (ret)
589 return ret;
590
Harald Welte2cf161b2009-06-20 22:36:41 +0200591 e1h = talloc(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200592 if (!e1h)
593 return -ENOMEM;
Harald Welte5fd8a542009-02-13 02:43:36 +0000594 memset(e1h, 0, sizeof(*e1h));
Harald Weltea4ffea92009-06-22 01:36:25 +0200595
Harald Welteedb37782009-05-01 14:59:07 +0000596 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000597
Harald Welte5c1e4582009-02-15 11:57:29 +0000598 /* Listen for OML connections */
Harald Welteedb37782009-05-01 14:59:07 +0000599 ret = make_sock(&e1h->listen_fd, 3002, listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000600 if (ret < 0)
601 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000602
Harald Welte5c1e4582009-02-15 11:57:29 +0000603 /* Listen for RSL connections */
Harald Welteedb37782009-05-01 14:59:07 +0000604 ret = make_sock(&e1h->rsl_listen_fd, 3003, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000605
Harald Welteedb37782009-05-01 14:59:07 +0000606 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000607}