blob: a6d09b31087fe3e207935c6e1927a9b7b0a3d540 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis input driver for ip.access */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by Holger Hans Peter Freyther
5 * (C) 2010 by On-Waves
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "internal.h"
25
26#include <stdio.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <errno.h>
30#include <string.h>
31#include <time.h>
32#include <sys/fcntl.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <arpa/inet.h>
36
37#include <osmocom/core/select.h>
38#include <osmocom/gsm/tlv.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/logging.h>
41#include <talloc.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020042#include <osmocom/abis/e1_input.h>
43#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020044#include <osmocom/core/socket.h>
Pablo Neira Ayuso0b099b22011-06-09 13:14:11 +020045#include <osmocom/abis/logging.h>
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +020046#include <osmocom/abis/ipa.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020047
48#define PRIV_OML 1
49#define PRIV_RSL 2
50
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020051static void *tall_ipa_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020053#define TS1_ALLOC_SIZE 900
54
55/*
56 * Common propietary IPA messages:
57 * - PONG: in reply to PING.
58 * - ID_REQUEST: first messages once OML has been established.
59 * - ID_ACK: in reply to ID_ACK.
60 */
61const uint8_t ipa_pong_msg[] = {
62 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
63};
64
65const uint8_t ipa_id_ack_msg[] = {
66 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
67};
68
69const uint8_t ipa_id_req_msg[] = {
70 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
71 0x01, IPAC_IDTAG_UNIT,
72 0x01, IPAC_IDTAG_MACADDR,
73 0x01, IPAC_IDTAG_LOCATION1,
74 0x01, IPAC_IDTAG_LOCATION2,
75 0x01, IPAC_IDTAG_EQUIPVERS,
76 0x01, IPAC_IDTAG_SWVERSION,
77 0x01, IPAC_IDTAG_UNITNAME,
78 0x01, IPAC_IDTAG_SERNR,
79};
80
81static int ipaccess_send(int fd, const void *msg, size_t msglen)
82{
83 int ret;
84
85 ret = write(fd, msg, msglen);
86 if (ret < 0)
87 return ret;
88 if (ret < msglen) {
89 LOGP(DINP, LOGL_ERROR, "ipaccess_send: short write\n");
90 return -EIO;
91 }
92 return ret;
93}
94
95int ipaccess_send_pong(int fd)
96{
97 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
98}
99
100int ipaccess_send_id_ack(int fd)
101{
102 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
103}
104
105int ipaccess_send_id_req(int fd)
106{
107 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
108}
109
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200110/* base handling of the ip.access protocol */
111int ipaccess_rcvmsg_base(struct msgb *msg,
112 struct osmo_fd *bfd)
113{
114 uint8_t msg_type = *(msg->l2h);
115 int ret = 0;
116
117 switch (msg_type) {
118 case IPAC_MSGT_PING:
119 ret = ipaccess_send_pong(bfd->fd);
120 break;
121 case IPAC_MSGT_PONG:
122 DEBUGP(DMI, "PONG!\n");
123 break;
124 case IPAC_MSGT_ID_ACK:
125 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
126 ret = ipaccess_send_id_ack(bfd->fd);
127 break;
128 }
129 return 0;
130}
131
132static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
133 struct osmo_fd *bfd)
134{
135 uint8_t msg_type = *(msg->l2h);
136
137 /* handle base messages */
138 ipaccess_rcvmsg_base(msg, bfd);
139
140 switch (msg_type) {
141 case IPAC_MSGT_ID_RESP:
142 DEBUGP(DMI, "ID_RESP\n");
143 if (!line->ops.sign_link_up) {
144 LOGP(DINP, LOGL_ERROR, "Fix your application, "
145 "no action set if the signalling link "
146 "becomes ready\n");
147 return -EINVAL;
148 }
149 line->ops.sign_link_up(msg, line);
150 break;
151 }
152 return 0;
153}
154
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200155static int handle_ts1_read(struct osmo_fd *bfd)
156{
157 struct e1inp_line *line = bfd->data;
158 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200159 struct e1inp_ts *e1i_ts = NULL;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200160 struct e1inp_sign_link *link;
161 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200162 struct msgb *msg;
163 int ret = 0, error;
164
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200165 error = ipa_msg_recv(bfd->fd, &msg);
166 if (error <= 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200167 /* skip if RSL line is not set yet. */
168 if (e1i_ts && e1i_ts->line->ops.error)
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200169 e1i_ts->line->ops.error(NULL, error);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200170 if (error == 0) {
171 osmo_fd_unregister(bfd);
172 close(bfd->fd);
173 bfd->fd = -1;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200174 talloc_free(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200175 }
176 return error;
177 }
178 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
179
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200180 /* Now that we're sure that we've got a line for socket. */
181 e1i_ts = &line->ts[ts_nr-1];
182
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200183 hh = (struct ipaccess_head *) msg->data;
184 if (hh->proto == IPAC_PROTO_IPACCESS) {
185 ipaccess_rcvmsg(line, msg, bfd);
186 msgb_free(msg);
187 return ret;
188 }
189 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
190 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200191
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200192 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
193 if (!link) {
194 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
195 "hh->proto=0x%02x\n", hh->proto);
196 msgb_free(msg);
197 return -EIO;
198 }
199 msg->dst = link;
200
201 /* XXX better use e1inp_ts_rx? */
202 if (!e1i_ts->line->ops.sign_link) {
203 LOGP(DINP, LOGL_ERROR, "Fix your application, "
204 "no action set for signalling messages.\n");
205 return -ENOENT;
206 }
207 e1i_ts->line->ops.sign_link(msg, link);
208
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200209 return ret;
210}
211
212void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
213{
214 struct ipaccess_head_ext *hh_ext;
215
216 /* prepend the osmo ip.access header extension */
217 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
218 hh_ext->proto = proto;
219}
220
221void ipaccess_prepend_header(struct msgb *msg, int proto)
222{
223 struct ipaccess_head *hh;
224
225 /* prepend the ip.access header */
226 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
227 hh->len = htons(msg->len - sizeof(*hh));
228 hh->proto = proto;
229}
230
231static int ts_want_write(struct e1inp_ts *e1i_ts)
232{
233 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
234
235 return 0;
236}
237
238static void timeout_ts1_write(void *data)
239{
240 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
241
242 /* trigger write of ts1, due to tx delay timer */
243 ts_want_write(e1i_ts);
244}
245
246static int handle_ts1_write(struct osmo_fd *bfd)
247{
248 struct e1inp_line *line = bfd->data;
249 unsigned int ts_nr = bfd->priv_nr;
250 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
251 struct e1inp_sign_link *sign_link;
252 struct msgb *msg;
253 uint8_t proto;
254 int ret;
255
256 bfd->when &= ~BSC_FD_WRITE;
257
258 /* get the next msg for this timeslot */
259 msg = e1inp_tx_ts(e1i_ts, &sign_link);
260 if (!msg) {
261 /* no message after tx delay timer */
262 return 0;
263 }
264
265 switch (sign_link->type) {
266 case E1INP_SIGN_OML:
267 proto = IPAC_PROTO_OML;
268 break;
269 case E1INP_SIGN_RSL:
270 proto = IPAC_PROTO_RSL;
271 break;
272 default:
273 msgb_free(msg);
274 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
275 return -EINVAL;
276 }
277
278 msg->l2h = msg->data;
279 ipaccess_prepend_header(msg, sign_link->tei);
280
281 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
282
283 ret = send(bfd->fd, msg->data, msg->len, 0);
284 msgb_free(msg);
285
286 /* set tx delay timer for next event */
287 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
288 e1i_ts->sign.tx_timer.data = e1i_ts;
289
290 /* Reducing this might break the nanoBTS 900 init. */
291 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
292
293 return ret;
294}
295
296/* callback from select.c in case one of the fd's can be read/written */
297static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
298{
299 struct e1inp_line *line = bfd->data;
300 unsigned int ts_nr = bfd->priv_nr;
301 unsigned int idx = ts_nr-1;
302 struct e1inp_ts *e1i_ts;
303 int rc = 0;
304
305 /* In case of early RSL we might not yet have a line */
306
307 if (line)
308 e1i_ts = &line->ts[idx];
309
310 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
311 if (what & BSC_FD_READ)
312 rc = handle_ts1_read(bfd);
313 if (what & BSC_FD_WRITE)
314 rc = handle_ts1_write(bfd);
315 } else
316 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
317
318 return rc;
319}
320
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200321static int ipaccess_line_update(struct e1inp_line *line,
322 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200323
324struct e1inp_driver ipaccess_driver = {
325 .name = "ipa",
326 .want_write = ts_want_write,
327 .line_update = ipaccess_line_update,
328 .default_delay = 0,
329};
330
331/* callback of the OML listening filedescriptor */
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200332static int ipaccess_bsc_oml_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200333{
334 int ret;
335 int idx = 0;
336 int i;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200337 struct e1inp_line *line = link->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200338 struct e1inp_ts *e1i_ts;
339 struct osmo_fd *bfd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200340
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200341 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
342 if (!bfd)
343 return -ENOMEM;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200344
345 /* create virrtual E1 timeslots for signalling */
346 e1inp_ts_config_sign(&line->ts[1-1], line);
347
348 /* initialize the fds */
349 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
350 line->ts[i].driver.ipaccess.fd.fd = -1;
351
352 e1i_ts = &line->ts[idx];
353
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200354 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355 bfd->data = line;
356 bfd->priv_nr = PRIV_OML;
357 bfd->cb = ipaccess_fd_cb;
358 bfd->when = BSC_FD_READ;
359 ret = osmo_fd_register(bfd);
360 if (ret < 0) {
361 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
362 close(bfd->fd);
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200363 talloc_free(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200364 return ret;
365 }
366
367 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
368 ret = ipaccess_send_id_req(bfd->fd);
369
370 return ret;
371}
372
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200373static int ipaccess_bsc_rsl_cb(struct ipa_server_link *link, int fd)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200374{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200375 struct osmo_fd *bfd;
376 int ret;
377
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200378 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200379 if (!bfd)
380 return -ENOMEM;
381
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200382 bfd->fd = fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200383 bfd->priv_nr = PRIV_RSL;
384 bfd->cb = ipaccess_fd_cb;
385 bfd->when = BSC_FD_READ;
386 ret = osmo_fd_register(bfd);
387 if (ret < 0) {
388 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
389 close(bfd->fd);
390 talloc_free(bfd);
391 return ret;
392 }
393 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
394 ret = ipaccess_send_id_req(bfd->fd);
395
396 return 0;
397}
398
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200399static int ipaccess_bts_cb(struct ipa_client_link *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200400{
401 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
402 struct e1inp_ts *e1i_ts = NULL;
403 struct e1inp_sign_link *sign_link;
404
405 /* special handling for IPA CCM. */
406 if (hh->proto == IPAC_PROTO_IPACCESS) {
407 uint8_t msg_type = *(msg->l2h);
408
409 /* ping, pong and acknowledgment cases. */
410 ipaccess_rcvmsg_base(msg, &link->ofd);
411
412 /* this is a request for identification from the BSC. */
413 if (msg_type == IPAC_MSGT_ID_GET) {
414 LOGP(DINP, LOGL_NOTICE, "received ID get\n");
415 if (!link->line->ops.sign_link_up) {
416 LOGP(DINP, LOGL_ERROR, "Fix your application, "
417 "no action set if the signalling link "
418 "becomes ready\n");
419 return -EINVAL;
420 }
421 link->line->ops.sign_link_up(msg, link->line);
422 }
423 return 0;
424 } else if (link->port == IPA_TCP_PORT_OML)
425 e1i_ts = &link->line->ts[0];
426 else if (link->port == IPA_TCP_PORT_RSL)
427 e1i_ts = &link->line->ts[1];
428
429 /* look up for some existing signaling link. */
430 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
431 if (sign_link == NULL) {
432 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
433 "hh->proto=0x%02x\n", hh->proto);
434 msgb_free(msg);
435 return -EIO;
436 }
437 msg->dst = sign_link;
438
439 /* XXX better use e1inp_ts_rx? */
440 if (!link->line->ops.sign_link) {
441 LOGP(DINP, LOGL_ERROR, "Fix your application, "
442 "no action set for signalling messages.\n");
443 return -ENOENT;
444 }
445 link->line->ops.sign_link(msg, sign_link);
446 return 0;
447}
448
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200449static int ipaccess_line_update(struct e1inp_line *line,
450 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200451{
452 int ret = -ENOENT;
453
454 switch(role) {
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200455 case E1INP_LINE_R_BSC: {
456 struct ipa_server_link *oml_link, *rsl_link;
457
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200458 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
459
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200460 oml_link = ipa_server_link_create(tall_ipa_ctx, line,
461 "0.0.0.0", IPA_TCP_PORT_OML,
462 ipaccess_bsc_oml_cb);
463 if (oml_link == NULL) {
464 LOGP(DINP, LOGL_ERROR, "cannot create OML "
465 "BSC link: %s\n", strerror(errno));
466 return -ENOMEM;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200467 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200468 if (ipa_server_link_open(oml_link) < 0) {
469 LOGP(DINP, LOGL_ERROR, "cannot open OML BSC link: %s\n",
470 strerror(errno));
471 ipa_server_link_close(oml_link);
472 ipa_server_link_destroy(oml_link);
473 return -EIO;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200474 }
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200475 rsl_link = ipa_server_link_create(tall_ipa_ctx, line,
476 "0.0.0.0", IPA_TCP_PORT_RSL,
477 ipaccess_bsc_rsl_cb);
478 if (rsl_link == NULL) {
479 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
480 "BSC link: %s\n", strerror(errno));
481 return -ENOMEM;
482 }
483 if (ipa_server_link_open(rsl_link) < 0) {
484 LOGP(DINP, LOGL_ERROR, "cannot open RSL BSC link: %s\n",
485 strerror(errno));
486 ipa_server_link_close(rsl_link);
487 ipa_server_link_destroy(rsl_link);
488 return -EIO;
489 }
490 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491 break;
Pablo Neira Ayuso986191f2011-06-21 19:56:26 +0200492 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200493 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200494 struct ipa_client_link *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200495
496 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
497
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200498 link = ipa_client_link_create(tall_ipa_ctx, line,
499 addr, IPA_TCP_PORT_OML,
500 ipaccess_bts_cb);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200501 if (link == NULL) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200502 LOGP(DINP, LOGL_ERROR, "cannot create OML "
503 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200504 return -ENOMEM;
505 }
506 if (ipa_client_link_open(link) < 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200507 LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
508 strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200509 ipa_client_link_close(link);
510 ipa_client_link_destroy(link);
511 return -EIO;
512 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200513 rsl_link = ipa_client_link_create(tall_ipa_ctx, line,
514 addr, IPA_TCP_PORT_RSL,
515 ipaccess_bts_cb);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200516 if (rsl_link == NULL) {
517 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
518 "BTS link: %s\n", strerror(errno));
519 return -ENOMEM;
520 }
521 if (ipa_client_link_open(rsl_link) < 0) {
522 LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
523 strerror(errno));
524 ipa_client_link_close(rsl_link);
525 ipa_client_link_destroy(rsl_link);
526 return -EIO;
527 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200528 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200530 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200531 default:
532 break;
533 }
534 return ret;
535}
536
537/* Actively connect to a BTS. Currently used by ipaccess-config.c */
538int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
539{
540 struct e1inp_ts *e1i_ts = &line->ts[0];
541 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
542 int ret, on = 1;
543
544 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
545 bfd->cb = ipaccess_fd_cb;
546 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
547 bfd->data = line;
548 bfd->priv_nr = PRIV_OML;
549
550 if (bfd->fd < 0) {
551 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
552 return -EIO;
553 }
554
555 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
556
557 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
558 if (ret < 0) {
559 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
560 close(bfd->fd);
561 return ret;
562 }
563
564 ret = osmo_fd_register(bfd);
565 if (ret < 0) {
566 close(bfd->fd);
567 return ret;
568 }
569
570 line->driver = &ipaccess_driver;
571
572 return ret;
573 //return e1inp_line_register(line);
574}
575
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200576void e1inp_ipaccess_init(void)
577{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200578 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200579 e1inp_driver_register(&ipaccess_driver);
580}