blob: 2239cf15263fb84a258c91daf920247ec3684380 [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
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200167/* base handling of the ip.access protocol */
168int ipaccess_rcvmsg_base(struct msgb *msg,
169 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000170{
Harald Welte5fd8a542009-02-13 02:43:36 +0000171 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000172 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000173
Harald Welte5fd8a542009-02-13 02:43:36 +0000174 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000175 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000176 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000177 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000178 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000179 DEBUGP(DMI, "PONG!\n");
180 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200181 case IPAC_MSGT_ID_ACK:
182 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
183 ret = write(bfd->fd, id_ack, sizeof(id_ack));
184 break;
185 }
186 return 0;
187}
188
189static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
190 struct bsc_fd *bfd)
191{
192 struct tlv_parsed tlvp;
193 u_int8_t msg_type = *(msg->l2h);
194 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
195 struct gsm_bts *bts;
196
197 /* handle base messages */
198 ipaccess_rcvmsg_base(msg, bfd);
199
200 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000201 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000202 DEBUGP(DMI, "ID_RESP ");
203 /* parse tags, search for Unit ID */
204 ipac_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
205 msgb_l2len(msg)-2);
206 DEBUGP(DMI, "\n");
207
208 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
209 break;
210
211 /* lookup BTS, create sign_link, ... */
212 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
213 &site_id, &bts_id, &trx_id);
214 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
215 if (!bts) {
216 DEBUGP(DINP, "Unable to find BTS configuration for "
217 " %u/%u/%u, disconnecting\n", site_id, bts_id,
218 trx_id);
219 return -EIO;
220 }
221 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
222 if (bfd->priv_nr == 1) {
223 bts->oml_link = e1inp_sign_link_create(&line->ts[1-1],
224 E1INP_SIGN_OML, bts->c0,
225 0, 0xff);
226 } else if (bfd->priv_nr == 2) {
227 struct e1inp_ts *e1i_ts;
228 struct bsc_fd *newbfd;
229
230 /* FIXME: implement this for non-0 TRX */
231 bfd->data = line = bts->oml_link->ts->line;
232 e1i_ts = &line->ts[2-1];
233 newbfd = &e1i_ts->driver.ipaccess.fd;
234
235 bts->c0->rsl_link =
236 e1inp_sign_link_create(e1i_ts,
237 E1INP_SIGN_RSL, bts->c0,
238 0, 0);
239 /* get rid of our old temporary bfd */
240 memcpy(newbfd, bfd, sizeof(*newbfd));
241 bsc_unregister_fd(bfd);
242 bsc_register_fd(newbfd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200243 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000244 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000245 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000246 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000247 return 0;
248}
249
Harald Welte7782c142009-02-15 03:39:51 +0000250/* FIXME: this is per BTS */
251static int oml_up = 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000252static int rsl_up = 0;
Harald Welte7782c142009-02-15 03:39:51 +0000253
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200254/*
255 * read one ipa message from the socket
256 * return NULL in case of error
257 */
258struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000259{
Harald Welte966636f2009-06-26 19:39:35 +0200260 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000261 struct ipaccess_head *hh;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200262 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000263
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200264 if (!msg) {
265 *error = -ENOMEM;
266 return NULL;
267 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000268
269 /* first read our 3-byte header */
270 hh = (struct ipaccess_head *) msg->data;
271 ret = recv(bfd->fd, msg->data, 3, 0);
272 if (ret < 0) {
273 fprintf(stderr, "recv error %s\n", strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200274 msgb_free(msg);
275 *error = ret;
276 return NULL;
277 } else if (ret == 0) {
278 msgb_free(msg);
279 *error = ret;
280 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000281 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200282
Harald Welte5fd8a542009-02-13 02:43:36 +0000283 msgb_put(msg, ret);
284
285 /* then read te length as specified in header */
286 msg->l2h = msg->data + sizeof(*hh);
287 ret = recv(bfd->fd, msg->l2h, hh->len, 0);
288 if (ret < hh->len) {
289 fprintf(stderr, "short read!\n");
Harald Welte5c1e4582009-02-15 11:57:29 +0000290 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200291 *error = -EIO;
292 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000293 }
294 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200295
296 return msg;
297}
298
299static int handle_ts1_read(struct bsc_fd *bfd)
300{
301 struct e1inp_line *line = bfd->data;
302 unsigned int ts_nr = bfd->priv_nr;
303 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
304 struct e1inp_sign_link *link;
305 struct msgb *msg;
306 struct ipaccess_head *hh;
307 int ret, error;
308
309 msg = ipaccess_read_msg(bfd, &error);
310 if (!msg) {
311 if (error == 0) {
312 fprintf(stderr, "BTS disappeared, dead socket\n");
313 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
314 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
315 bsc_unregister_fd(bfd);
316 close(bfd->fd);
317 bfd->fd = -1;
318 }
319 return error;
320 }
321
Sylvain Munaut86538c72009-09-30 22:35:04 +0200322 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000323
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200324 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000325 if (hh->proto == IPAC_PROTO_IPACCESS) {
326 ret = ipaccess_rcvmsg(line, msg, bfd);
327 if (ret < 0) {
328 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_RSL);
329 e1inp_event(e1i_ts, EVT_E1_TEI_DN, 0, IPAC_PROTO_OML);
330 bsc_unregister_fd(bfd);
331 close(bfd->fd);
332 bfd->fd = -1;
333 }
334 msgb_free(msg);
335 return ret;
336 }
337 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
338 * might have free'd it !!! */
339
Harald Welte5fd8a542009-02-13 02:43:36 +0000340 link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
341 if (!link) {
342 printf("no matching signalling link for hh->proto=0x%02x\n", hh->proto);
343 msgb_free(msg);
344 return -EIO;
345 }
346 msg->trx = link->trx;
347
348 switch (hh->proto) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000349 case IPAC_PROTO_RSL:
Harald Welte5c1e4582009-02-15 11:57:29 +0000350 if (!rsl_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000351 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_RSL);
Harald Welte5c1e4582009-02-15 11:57:29 +0000352 rsl_up = 1;
353 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000354 ret = abis_rsl_rcvmsg(msg);
355 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000356 case IPAC_PROTO_OML:
Harald Welte7782c142009-02-15 03:39:51 +0000357 if (!oml_up) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000358 e1inp_event(e1i_ts, EVT_E1_TEI_UP, 0, IPAC_PROTO_OML);
Harald Welte7782c142009-02-15 03:39:51 +0000359 oml_up = 1;
360 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000361 ret = abis_nm_rcvmsg(msg);
362 break;
363 default:
Harald Welte7782c142009-02-15 03:39:51 +0000364 DEBUGP(DMI, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000365 msgb_free(msg);
366 break;
367 }
368 return ret;
369}
370
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200371void ipaccess_prepend_header(struct msgb *msg, int proto)
372{
373 struct ipaccess_head *hh;
374
375 /* prepend the ip.access header */
376 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
377 hh->zero = 0;
378 hh->len = msg->len - sizeof(*hh);
379 hh->proto = proto;
380}
381
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200382static int ts_want_write(struct e1inp_ts *e1i_ts)
383{
384 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
385
386 return 0;
387}
388
389static void timeout_ts1_write(void *data)
390{
391 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
392
393 /* trigger write of ts1, due to tx delay timer */
394 ts_want_write(e1i_ts);
395}
396
Harald Welte5fd8a542009-02-13 02:43:36 +0000397static int handle_ts1_write(struct bsc_fd *bfd)
398{
399 struct e1inp_line *line = bfd->data;
400 unsigned int ts_nr = bfd->priv_nr;
401 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
402 struct e1inp_sign_link *sign_link;
403 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200404 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000405 int ret;
406
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200407 bfd->when &= ~BSC_FD_WRITE;
408
Harald Welte5fd8a542009-02-13 02:43:36 +0000409 /* get the next msg for this timeslot */
410 msg = e1inp_tx_ts(e1i_ts, &sign_link);
411 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200412 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000413 return 0;
414 }
415
Harald Welte5fd8a542009-02-13 02:43:36 +0000416 switch (sign_link->type) {
417 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200418 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000419 break;
420 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200421 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000422 break;
423 default:
424 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200425 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000426 return -EINVAL;
427 }
428
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200429
430 msg->l2h = msg->data;
431 ipaccess_prepend_header(msg, proto);
432
433 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000434
435 ret = send(bfd->fd, msg->data, msg->len, 0);
436 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200437
438 /* set tx delay timer for next event */
439 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
440 e1i_ts->sign.tx_timer.data = e1i_ts;
441 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000442
443 return ret;
444}
445
Harald Welte5fd8a542009-02-13 02:43:36 +0000446/* callback from select.c in case one of the fd's can be read/written */
447static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
448{
449 struct e1inp_line *line = bfd->data;
450 unsigned int ts_nr = bfd->priv_nr;
451 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000452 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000453 int rc = 0;
454
Harald Welteedb37782009-05-01 14:59:07 +0000455 /* In case of early RSL we might not yet have a line */
456
457 if (line)
458 e1i_ts = &line->ts[idx];
459
460 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000461 if (what & BSC_FD_READ)
462 rc = handle_ts1_read(bfd);
463 if (what & BSC_FD_WRITE)
464 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000465 } else
Harald Welte5fd8a542009-02-13 02:43:36 +0000466 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000467
468 return rc;
469}
470
471
Harald Welte5fd8a542009-02-13 02:43:36 +0000472struct e1inp_driver ipaccess_driver = {
473 .name = "ip.access",
474 .want_write = ts_want_write,
475};
476
Harald Welteedb37782009-05-01 14:59:07 +0000477/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000478static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
479{
Harald Welte5fd8a542009-02-13 02:43:36 +0000480 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000481 int idx = 0;
482 struct e1inp_line *line;
483 struct e1inp_ts *e1i_ts;
484 struct bsc_fd *bfd;
485 struct sockaddr_in sa;
486 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000487
Harald Welteedb37782009-05-01 14:59:07 +0000488 if (!(what & BSC_FD_READ))
489 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000490
Harald Welteedb37782009-05-01 14:59:07 +0000491 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
492 if (ret < 0) {
493 perror("accept");
494 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000495 }
Harald Welteedb37782009-05-01 14:59:07 +0000496 DEBUGP(DINP, "accept()ed new OML link from %s\n", inet_ntoa(sa.sin_addr));
497
Harald Welte2cf161b2009-06-20 22:36:41 +0200498 line = talloc(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000499 if (!line) {
500 close(ret);
501 return -ENOMEM;
502 }
503 memset(line, 0, sizeof(*line));
504 line->driver = &ipaccess_driver;
505 //line->driver_data = e1h;
506 /* create virrtual E1 timeslots for signalling */
507 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
508 e1inp_ts_config(&line->ts[2-1], line, E1INP_TS_TYPE_SIGN);
509
510 e1i_ts = &line->ts[idx];
511
512 bfd = &e1i_ts->driver.ipaccess.fd;
513 bfd->fd = ret;
514 bfd->data = line;
515 bfd->priv_nr = 1;
516 bfd->cb = ipaccess_fd_cb;
517 bfd->when = BSC_FD_READ;
518 ret = bsc_register_fd(bfd);
519 if (ret < 0) {
520 fprintf(stderr, "could not register FD\n");
521 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200522 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000523 return ret;
524 }
525
526 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
527 ret = write(bfd->fd, id_req, sizeof(id_req));
528
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200529 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200530 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000531}
532
Harald Welte5c1e4582009-02-15 11:57:29 +0000533static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
534{
Harald Welteedb37782009-05-01 14:59:07 +0000535 struct sockaddr_in sa;
536 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200537 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000538 int ret;
539
Harald Welteedb37782009-05-01 14:59:07 +0000540 if (!(what & BSC_FD_READ))
541 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000542
Harald Weltea4ffea92009-06-22 01:36:25 +0200543 bfd = talloc(tall_bsc_ctx, struct bsc_fd);
544 if (!bfd)
545 return -ENOMEM;
546 memset(bfd, 0, sizeof(*bfd));
547
Harald Welteedb37782009-05-01 14:59:07 +0000548 /* Some BTS has connected to us, but we don't know yet which line
549 * (as created by the OML link) to associate it with. Thus, we
550 * aloocate a temporary bfd until we have received ID from BTS */
551
552 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
553 if (bfd->fd < 0) {
554 perror("accept");
555 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000556 }
Harald Welteedb37782009-05-01 14:59:07 +0000557 DEBUGP(DINP, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
558 bfd->priv_nr = 2;
559 bfd->cb = ipaccess_fd_cb;
560 bfd->when = BSC_FD_READ;
561 ret = bsc_register_fd(bfd);
562 if (ret < 0) {
563 fprintf(stderr, "could not register FD\n");
564 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200565 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000566 return ret;
567 }
568 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
569 ret = write(bfd->fd, id_req, sizeof(id_req));
570
Harald Welte5c1e4582009-02-15 11:57:29 +0000571 return 0;
572}
573
574static int make_sock(struct bsc_fd *bfd, u_int16_t port,
Harald Welte5c1e4582009-02-15 11:57:29 +0000575 int (*cb)(struct bsc_fd *fd, unsigned int what))
Harald Welte5fd8a542009-02-13 02:43:36 +0000576{
577 struct sockaddr_in addr;
Harald Welte5c1e4582009-02-15 11:57:29 +0000578 int ret, on = 1;
579
580 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
581 bfd->cb = cb;
582 bfd->when = BSC_FD_READ;
Harald Welteedb37782009-05-01 14:59:07 +0000583 //bfd->data = line;
Harald Welte5c1e4582009-02-15 11:57:29 +0000584
585 memset(&addr, 0, sizeof(addr));
586 addr.sin_family = AF_INET;
587 addr.sin_port = htons(port);
588 addr.sin_addr.s_addr = INADDR_ANY;
589
590 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
591
592 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
593 if (ret < 0) {
594 fprintf(stderr, "could not bind l2 socket %s\n",
595 strerror(errno));
596 return -EIO;
597 }
598
599 ret = listen(bfd->fd, 1);
600 if (ret < 0) {
601 perror("listen");
602 return ret;
603 }
604
605 ret = bsc_register_fd(bfd);
606 if (ret < 0) {
607 perror("register_listen_fd");
608 return ret;
609 }
610 return 0;
611}
612
Harald Welte25de9912009-04-30 15:53:07 +0000613/* Actively connect to a BTS. Currently used by ipaccess-config.c */
614int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
615{
616 struct e1inp_ts *e1i_ts = &line->ts[0];
617 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
618 int ret, on = 1;
619
620 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
621 bfd->cb = ipaccess_fd_cb;
622 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
623 bfd->data = line;
624 bfd->priv_nr = 1;
625
626 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
627
628 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
629 if (ret < 0) {
630 fprintf(stderr, "could not connect socket\n");
631 close(bfd->fd);
632 return ret;
633 }
634
635 ret = bsc_register_fd(bfd);
636 if (ret < 0) {
637 close(bfd->fd);
638 return ret;
639 }
640
641 line->driver = &ipaccess_driver;
642
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200643 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200644 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000645}
646
Harald Welteedb37782009-05-01 14:59:07 +0000647int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000648{
Harald Welte5c1e4582009-02-15 11:57:29 +0000649 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000650
651 /* register the driver with the core */
652 /* FIXME: do this in the plugin initializer function */
653 ret = e1inp_driver_register(&ipaccess_driver);
654 if (ret)
655 return ret;
656
Harald Welte2cf161b2009-06-20 22:36:41 +0200657 e1h = talloc(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200658 if (!e1h)
659 return -ENOMEM;
Harald Welte5fd8a542009-02-13 02:43:36 +0000660 memset(e1h, 0, sizeof(*e1h));
Harald Weltea4ffea92009-06-22 01:36:25 +0200661
Harald Welteedb37782009-05-01 14:59:07 +0000662 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000663
Harald Welte5c1e4582009-02-15 11:57:29 +0000664 /* Listen for OML connections */
Harald Welteedb37782009-05-01 14:59:07 +0000665 ret = make_sock(&e1h->listen_fd, 3002, listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000666 if (ret < 0)
667 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000668
Harald Welte5c1e4582009-02-15 11:57:29 +0000669 /* Listen for RSL connections */
Harald Welteedb37782009-05-01 14:59:07 +0000670 ret = make_sock(&e1h->rsl_listen_fd, 3003, rsl_listen_fd_cb);
Harald Welte5fd8a542009-02-13 02:43:36 +0000671
Harald Welteedb37782009-05-01 14:59:07 +0000672 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000673}