blob: 5c9cd46be2b2cc270699a5dbc67c4f14f5e10cc6 [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 int i;
117
Harald Weltee441d9c2009-06-21 16:17:15 +0200118 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000119
120 if (!is_ipaccess_bts(bts))
121 continue;
122
123 if (bts->ip_access.site_id == site_id &&
124 bts->ip_access.bts_id == bts_id)
125 return bts;
126 }
127
128 return NULL;
129}
130
131static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
132 u_int16_t *trx_id)
133{
134 unsigned long ul;
135 char *endptr;
136 const char *nptr;
137
138 nptr = str;
139 ul = strtoul(nptr, &endptr, 10);
140 if (endptr <= nptr)
141 return -EINVAL;
142 if (site_id)
143 *site_id = ul & 0xffff;
144
145 if (*endptr++ != '/')
146 return -EINVAL;
147
148 nptr = endptr;
149 ul = strtoul(nptr, &endptr, 10);
150 if (endptr <= nptr)
151 return -EINVAL;
152 if (bts_id)
153 *bts_id = ul & 0xffff;
154
155 if (*endptr++ != '/')
156 return -EINVAL;
157
158 nptr = endptr;
159 ul = strtoul(nptr, &endptr, 10);
160 if (endptr <= nptr)
161 return -EINVAL;
162 if (trx_id)
163 *trx_id = ul & 0xffff;
164
165 return 0;
166}
167
168static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
169 struct bsc_fd *bfd)
170{
171 struct tlv_parsed tlvp;
Harald Welte5fd8a542009-02-13 02:43:36 +0000172 u_int8_t msg_type = *(msg->l2h);
Harald Welteedb37782009-05-01 14:59:07 +0000173 u_int16_t site_id, bts_id, trx_id;
174 struct gsm_bts *bts;
Holger Freytherff9592f2009-03-09 16:17:14 +0000175 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000176
Harald Welte5fd8a542009-02-13 02:43:36 +0000177 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000178 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000179 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000180 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000181 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000182 DEBUGP(DMI, "PONG!\n");
183 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000184 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000185 DEBUGP(DMI, "ID_RESP ");
186 /* parse tags, search for Unit ID */
187 ipac_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
188 msgb_l2len(msg)-2);
189 DEBUGP(DMI, "\n");
190
191 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
192 break;
193
194 /* lookup BTS, create sign_link, ... */
195 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
196 &site_id, &bts_id, &trx_id);
197 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
198 if (!bts) {
199 DEBUGP(DINP, "Unable to find BTS configuration for "
200 " %u/%u/%u, disconnecting\n", site_id, bts_id,
201 trx_id);
202 return -EIO;
203 }
204 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
205 if (bfd->priv_nr == 1) {
206 bts->oml_link = e1inp_sign_link_create(&line->ts[1-1],
207 E1INP_SIGN_OML, bts->c0,
208 0, 0xff);
209 } else if (bfd->priv_nr == 2) {
210 struct e1inp_ts *e1i_ts;
211 struct bsc_fd *newbfd;
212
213 /* FIXME: implement this for non-0 TRX */
214 bfd->data = line = bts->oml_link->ts->line;
215 e1i_ts = &line->ts[2-1];
216 newbfd = &e1i_ts->driver.ipaccess.fd;
217
218 bts->c0->rsl_link =
219 e1inp_sign_link_create(e1i_ts,
220 E1INP_SIGN_RSL, bts->c0,
221 0, 0);
222 /* get rid of our old temporary bfd */
223 memcpy(newbfd, bfd, sizeof(*newbfd));
224 bsc_unregister_fd(bfd);
225 bsc_register_fd(newbfd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200226 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000227 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000228 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000229 case IPAC_MSGT_ID_ACK:
Harald Welte7782c142009-02-15 03:39:51 +0000230 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Harald Welteedb37782009-05-01 14:59:07 +0000231 ret = write(bfd->fd, id_ack, sizeof(id_ack));
Harald Welte5fd8a542009-02-13 02:43:36 +0000232 break;
233 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000234 return 0;
235}
236
Harald Welte7782c142009-02-15 03:39:51 +0000237/* FIXME: this is per BTS */
238static int oml_up = 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000239static int rsl_up = 0;
Harald Welte7782c142009-02-15 03:39:51 +0000240
Harald Welte5fd8a542009-02-13 02:43:36 +0000241static int handle_ts1_read(struct bsc_fd *bfd)
242{
243 struct e1inp_line *line = bfd->data;
244 unsigned int ts_nr = bfd->priv_nr;
245 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
246 struct e1inp_sign_link *link;
Harald Welte966636f2009-06-26 19:39:35 +0200247 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000248 struct ipaccess_head *hh;
249 int ret;
250
251 if (!msg)
252 return -ENOMEM;
253
254 /* first read our 3-byte header */
255 hh = (struct ipaccess_head *) msg->data;
256 ret = recv(bfd->fd, msg->data, 3, 0);
257 if (ret < 0) {
258 fprintf(stderr, "recv error %s\n", strerror(errno));
259 return ret;
260 }
261 if (ret == 0) {
262 fprintf(stderr, "BTS disappeared, dead socket\n");
Harald Welte4f361fc2009-02-15 15:32:53 +0000263 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
264 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
Harald Welte5fd8a542009-02-13 02:43:36 +0000265 bsc_unregister_fd(bfd);
266 close(bfd->fd);
267 bfd->fd = -1;
268 }
269 msgb_put(msg, ret);
270
271 /* then read te length as specified in header */
272 msg->l2h = msg->data + sizeof(*hh);
273 ret = recv(bfd->fd, msg->l2h, hh->len, 0);
274 if (ret < hh->len) {
275 fprintf(stderr, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000276 msgb_free(msg);
277 return -EIO;
Harald Welte5fd8a542009-02-13 02:43:36 +0000278 }
279 msgb_put(msg, ret);
Harald Welte3cc4bf52009-02-28 13:08:01 +0000280 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), ret));
Harald Welte5fd8a542009-02-13 02:43:36 +0000281
Harald Welteedb37782009-05-01 14:59:07 +0000282 if (hh->proto == IPAC_PROTO_IPACCESS) {
283 ret = ipaccess_rcvmsg(line, msg, bfd);
284 if (ret < 0) {
285 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
286 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
287 bsc_unregister_fd(bfd);
288 close(bfd->fd);
289 bfd->fd = -1;
290 }
291 msgb_free(msg);
292 return ret;
293 }
294 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
295 * might have free'd it !!! */
296
Harald Welte5fd8a542009-02-13 02:43:36 +0000297 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
298 if (!link) {
299 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
300 msgb_free(msg);
301 return -EIO;
302 }
303 msg->trx = link->trx;
304
305 switch (hh->proto) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000306 case IPAC_PROTO_RSL:
Harald Welte5c1e4582009-02-15 11:57:29 +0000307 if (!rsl_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000308 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_RSL);
Harald Welte5c1e4582009-02-15 11:57:29 +0000309 rsl_up = 1;
310 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000311 ret = abis_rsl_rcvmsg(msg);
312 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000313 case IPAC_PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000314 if (!oml_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000315 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_OML);
Harald Welte7782c142009-02-15 03:39:51 +0000316 oml_up = 1;
317 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000318 ret = abis_nm_rcvmsg(msg);
319 break;
320 default:
Harald Welte7782c142009-02-15 03:39:51 +0000321 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000322 msgb_free(msg);
323 break;
324 }
325 return ret;
326}
327
328static int handle_ts1_write(struct bsc_fd *bfd)
329{
330 struct e1inp_line *line = bfd->data;
331 unsigned int ts_nr = bfd->priv_nr;
332 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
333 struct e1inp_sign_link *sign_link;
334 struct msgb *msg;
335 struct ipaccess_head *hh;
336 u_int8_t *l2_data;
337 int ret;
338
339 /* get the next msg for this timeslot */
340 msg = e1inp_tx_ts(e1i_ts, &sign_link);
341 if (!msg) {
342 bfd->when &= ~BSC_FD_WRITE;
343 return 0;
344 }
345
346 l2_data = msg->data;
347
Harald Welteedb37782009-05-01 14:59:07 +0000348 /* prepend the ip.access header */
Harald Welte5fd8a542009-02-13 02:43:36 +0000349 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
350 hh->zero = 0;
351 hh->len = msg->len - sizeof(*hh);
352
353 switch (sign_link->type) {
354 case E1INP_SIGN_OML:
Harald Welte4f361fc2009-02-15 15:32:53 +0000355 hh->proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000356 break;
357 case E1INP_SIGN_RSL:
Harald Welte4f361fc2009-02-15 15:32:53 +0000358 hh->proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000359 break;
360 default:
361 msgb_free(msg);
362 return -EINVAL;
363 }
364
Harald Welte3cc4bf52009-02-28 13:08:01 +0000365 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(l2_data, hh->len));
Harald Welte5fd8a542009-02-13 02:43:36 +0000366
367 ret = send(bfd->fd, msg->data, msg->len, 0);
368 msgb_free(msg);
Harald Welte7a2a71e2009-05-01 21:54:11 +0000369 usleep(100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000370
371 return ret;
372}
373
Harald Welte5fd8a542009-02-13 02:43:36 +0000374/* callback from select.c in case one of the fd's can be read/written */
375static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
376{
377 struct e1inp_line *line = bfd->data;
378 unsigned int ts_nr = bfd->priv_nr;
379 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000380 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000381 int rc = 0;
382
Harald Welteedb37782009-05-01 14:59:07 +0000383 /* In case of early RSL we might not yet have a line */
384
385 if (line)
386 e1i_ts = &line->ts[idx];
387
388 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000389 if (what & BSC_FD_READ)
390 rc = handle_ts1_read(bfd);
391 if (what & BSC_FD_WRITE)
392 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000393 } else
Harald Welte5fd8a542009-02-13 02:43:36 +0000394 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000395
396 return rc;
397}
398
399
400static int ts_want_write(struct e1inp_ts *e1i_ts)
401{
Harald Welte5fd8a542009-02-13 02:43:36 +0000402 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
403
404 return 0;
405}
406
407struct e1inp_driver ipaccess_driver = {
408 .name = "ip.access",
409 .want_write = ts_want_write,
410};
411
Harald Welteedb37782009-05-01 14:59:07 +0000412/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000413static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
414{
Harald Welte5fd8a542009-02-13 02:43:36 +0000415 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000416 int idx = 0;
417 struct e1inp_line *line;
418 struct e1inp_ts *e1i_ts;
419 struct bsc_fd *bfd;
420 struct sockaddr_in sa;
421 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000422
Harald Welteedb37782009-05-01 14:59:07 +0000423 if (!(what & BSC_FD_READ))
424 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000425
Harald Welteedb37782009-05-01 14:59:07 +0000426 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
427 if (ret < 0) {
428 perror("accept");
429 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000430 }
Harald Welteedb37782009-05-01 14:59:07 +0000431 DEBUGP(DINP, "accept()ed new OML link from %s\n", inet_ntoa(sa.sin_addr));
432
Harald Welte2cf161b2009-06-20 22:36:41 +0200433 line = talloc(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000434 if (!line) {
435 close(ret);
436 return -ENOMEM;
437 }
438 memset(line, 0, sizeof(*line));
439 line->driver = &ipaccess_driver;
440 //line->driver_data = e1h;
441 /* create virrtual E1 timeslots for signalling */
442 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
443 e1inp_ts_config(&line->ts[2-1], line, E1INP_TS_TYPE_SIGN);
444
445 e1i_ts = &line->ts[idx];
446
447 bfd = &e1i_ts->driver.ipaccess.fd;
448 bfd->fd = ret;
449 bfd->data = line;
450 bfd->priv_nr = 1;
451 bfd->cb = ipaccess_fd_cb;
452 bfd->when = BSC_FD_READ;
453 ret = bsc_register_fd(bfd);
454 if (ret < 0) {
455 fprintf(stderr, "could not register FD\n");
456 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200457 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000458 return ret;
459 }
460
461 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
462 ret = write(bfd->fd, id_req, sizeof(id_req));
463
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200464 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200465 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000466}
467
Harald Welte5c1e4582009-02-15 11:57:29 +0000468static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
469{
Harald Welteedb37782009-05-01 14:59:07 +0000470 struct sockaddr_in sa;
471 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200472 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000473 int ret;
474
Harald Welteedb37782009-05-01 14:59:07 +0000475 if (!(what & BSC_FD_READ))
476 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000477
Harald Weltea4ffea92009-06-22 01:36:25 +0200478 bfd = talloc(tall_bsc_ctx, struct bsc_fd);
479 if (!bfd)
480 return -ENOMEM;
481 memset(bfd, 0, sizeof(*bfd));
482
Harald Welteedb37782009-05-01 14:59:07 +0000483 /* Some BTS has connected to us, but we don't know yet which line
484 * (as created by the OML link) to associate it with. Thus, we
485 * aloocate a temporary bfd until we have received ID from BTS */
486
487 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
488 if (bfd->fd < 0) {
489 perror("accept");
490 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000491 }
Harald Welteedb37782009-05-01 14:59:07 +0000492 DEBUGP(DINP, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
493 bfd->priv_nr = 2;
494 bfd->cb = ipaccess_fd_cb;
495 bfd->when = BSC_FD_READ;
496 ret = bsc_register_fd(bfd);
497 if (ret < 0) {
498 fprintf(stderr, "could not register FD\n");
499 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200500 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000501 return ret;
502 }
503 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
504 ret = write(bfd->fd, id_req, sizeof(id_req));
505
Harald Welte5c1e4582009-02-15 11:57:29 +0000506 return 0;
507}
508
509static int make_sock(struct bsc_fd *bfd, u_int16_t port,
Harald Welte5c1e4582009-02-15 11:57:29 +0000510 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000511{
512 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000513 int ret, on = 1;
514
515 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
516 bfd->cb = cb;
517 bfd->when = BSC_FD_READ;
Harald Welteedb37782009-05-01 14:59:07 +0000518 //bfd->data = line;
Harald Welte5c1e4582009-02-15 11:57:29 +0000519
520 memset(&addr, 0, sizeof(addr));
521 addr.sin_family = AF_INET;
522 addr.sin_port = htons(port);
523 addr.sin_addr.s_addr = INADDR_ANY;
524
525 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
526
527 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
528 if (ret < 0) {
529 fprintf(stderr, "could not bind l2 socket %s\n",
530 strerror(errno));
531 return -EIO;
532 }
533
534 ret = listen(bfd->fd, 1);
535 if (ret < 0) {
536 perror("listen");
537 return ret;
538 }
539
540 ret = bsc_register_fd(bfd);
541 if (ret < 0) {
542 perror("register_listen_fd");
543 return ret;
544 }
545 return 0;
546}
547
Harald Welte25de9912009-04-30 15:53:07 +0000548/* Actively connect to a BTS. Currently used by ipaccess-config.c */
549int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
550{
551 struct e1inp_ts *e1i_ts = &line->ts[0];
552 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
553 int ret, on = 1;
554
555 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
556 bfd->cb = ipaccess_fd_cb;
557 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
558 bfd->data = line;
559 bfd->priv_nr = 1;
560
561 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
562
563 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
564 if (ret < 0) {
565 fprintf(stderr, "could not connect socket\n");
566 close(bfd->fd);
567 return ret;
568 }
569
570 ret = bsc_register_fd(bfd);
571 if (ret < 0) {
572 close(bfd->fd);
573 return ret;
574 }
575
576 line->driver = &ipaccess_driver;
577
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200578 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200579 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000580}
581
Harald Welteedb37782009-05-01 14:59:07 +0000582int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000583{
Harald Welte5c1e4582009-02-15 11:57:29 +0000584 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000585
586 /* register the driver with the core */
587 /* FIXME: do this in the plugin initializer function */
588 ret = e1inp_driver_register(&ipaccess_driver);
589 if (ret)
590 return ret;
591
Harald Welte2cf161b2009-06-20 22:36:41 +0200592 e1h = talloc(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200593 if (!e1h)
594 return -ENOMEM;
Harald Welte5fd8a542009-02-13 02:43:36 +0000595 memset(e1h, 0, sizeof(*e1h));
Harald Weltea4ffea92009-06-22 01:36:25 +0200596
Harald Welteedb37782009-05-01 14:59:07 +0000597 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000598
Harald Welte5c1e4582009-02-15 11:57:29 +0000599 /* Listen for OML connections */
Harald Welteedb37782009-05-01 14:59:07 +0000600 ret = make_sock(&e1h->listen_fd, 3002, listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000601 if (ret < 0)
602 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000603
Harald Welte5c1e4582009-02-15 11:57:29 +0000604 /* Listen for RSL connections */
Harald Welteedb37782009-05-01 14:59:07 +0000605 ret = make_sock(&e1h->rsl_listen_fd, 3003, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000606
Harald Welteedb37782009-05-01 14:59:07 +0000607 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000608}