blob: 60e71099a6f2ca7f7466c275c157c94847f7995b [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
53/* data structure for one E1 interface with A-bis */
54struct ia_e1_handle {
55 struct osmo_fd listen_fd;
56 struct osmo_fd rsl_listen_fd;
57 struct gsm_network *gsmnet;
58};
59
60static struct ia_e1_handle *e1h;
61
62
63#define TS1_ALLOC_SIZE 900
64
65/*
66 * Common propietary IPA messages:
67 * - PONG: in reply to PING.
68 * - ID_REQUEST: first messages once OML has been established.
69 * - ID_ACK: in reply to ID_ACK.
70 */
71const uint8_t ipa_pong_msg[] = {
72 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
73};
74
75const uint8_t ipa_id_ack_msg[] = {
76 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
77};
78
79const uint8_t ipa_id_req_msg[] = {
80 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
81 0x01, IPAC_IDTAG_UNIT,
82 0x01, IPAC_IDTAG_MACADDR,
83 0x01, IPAC_IDTAG_LOCATION1,
84 0x01, IPAC_IDTAG_LOCATION2,
85 0x01, IPAC_IDTAG_EQUIPVERS,
86 0x01, IPAC_IDTAG_SWVERSION,
87 0x01, IPAC_IDTAG_UNITNAME,
88 0x01, IPAC_IDTAG_SERNR,
89};
90
91static int ipaccess_send(int fd, const void *msg, size_t msglen)
92{
93 int ret;
94
95 ret = write(fd, msg, msglen);
96 if (ret < 0)
97 return ret;
98 if (ret < msglen) {
99 LOGP(DINP, LOGL_ERROR, "ipaccess_send: short write\n");
100 return -EIO;
101 }
102 return ret;
103}
104
105int ipaccess_send_pong(int fd)
106{
107 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
108}
109
110int ipaccess_send_id_ack(int fd)
111{
112 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
113}
114
115int ipaccess_send_id_req(int fd)
116{
117 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
118}
119
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200120/* base handling of the ip.access protocol */
121int ipaccess_rcvmsg_base(struct msgb *msg,
122 struct osmo_fd *bfd)
123{
124 uint8_t msg_type = *(msg->l2h);
125 int ret = 0;
126
127 switch (msg_type) {
128 case IPAC_MSGT_PING:
129 ret = ipaccess_send_pong(bfd->fd);
130 break;
131 case IPAC_MSGT_PONG:
132 DEBUGP(DMI, "PONG!\n");
133 break;
134 case IPAC_MSGT_ID_ACK:
135 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
136 ret = ipaccess_send_id_ack(bfd->fd);
137 break;
138 }
139 return 0;
140}
141
142static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
143 struct osmo_fd *bfd)
144{
145 uint8_t msg_type = *(msg->l2h);
146
147 /* handle base messages */
148 ipaccess_rcvmsg_base(msg, bfd);
149
150 switch (msg_type) {
151 case IPAC_MSGT_ID_RESP:
152 DEBUGP(DMI, "ID_RESP\n");
153 if (!line->ops.sign_link_up) {
154 LOGP(DINP, LOGL_ERROR, "Fix your application, "
155 "no action set if the signalling link "
156 "becomes ready\n");
157 return -EINVAL;
158 }
159 line->ops.sign_link_up(msg, line);
160 break;
161 }
162 return 0;
163}
164
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200165static int handle_ts1_read(struct osmo_fd *bfd)
166{
167 struct e1inp_line *line = bfd->data;
168 unsigned int ts_nr = bfd->priv_nr;
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200169 struct e1inp_ts *e1i_ts = NULL;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200170 struct e1inp_sign_link *link;
171 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200172 struct msgb *msg;
173 int ret = 0, error;
174
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200175 error = ipa_msg_recv(bfd->fd, &msg);
176 if (error <= 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200177 /* skip if RSL line is not set yet. */
178 if (e1i_ts && e1i_ts->line->ops.error)
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200179 e1i_ts->line->ops.error(NULL, error);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200180 if (error == 0) {
181 osmo_fd_unregister(bfd);
182 close(bfd->fd);
183 bfd->fd = -1;
184 }
185 return error;
186 }
187 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
188
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200189 /* Now that we're sure that we've got a line for socket. */
190 e1i_ts = &line->ts[ts_nr-1];
191
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200192 hh = (struct ipaccess_head *) msg->data;
193 if (hh->proto == IPAC_PROTO_IPACCESS) {
194 ipaccess_rcvmsg(line, msg, bfd);
195 msgb_free(msg);
196 return ret;
197 }
198 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
199 * might have free'd it !!! */
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200200
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200201 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
202 if (!link) {
203 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
204 "hh->proto=0x%02x\n", hh->proto);
205 msgb_free(msg);
206 return -EIO;
207 }
208 msg->dst = link;
209
210 /* XXX better use e1inp_ts_rx? */
211 if (!e1i_ts->line->ops.sign_link) {
212 LOGP(DINP, LOGL_ERROR, "Fix your application, "
213 "no action set for signalling messages.\n");
214 return -ENOENT;
215 }
216 e1i_ts->line->ops.sign_link(msg, link);
217
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200218 return ret;
219}
220
221void ipaccess_prepend_header_ext(struct msgb *msg, int proto)
222{
223 struct ipaccess_head_ext *hh_ext;
224
225 /* prepend the osmo ip.access header extension */
226 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
227 hh_ext->proto = proto;
228}
229
230void ipaccess_prepend_header(struct msgb *msg, int proto)
231{
232 struct ipaccess_head *hh;
233
234 /* prepend the ip.access header */
235 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
236 hh->len = htons(msg->len - sizeof(*hh));
237 hh->proto = proto;
238}
239
240static int ts_want_write(struct e1inp_ts *e1i_ts)
241{
242 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
243
244 return 0;
245}
246
247static void timeout_ts1_write(void *data)
248{
249 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
250
251 /* trigger write of ts1, due to tx delay timer */
252 ts_want_write(e1i_ts);
253}
254
255static int handle_ts1_write(struct osmo_fd *bfd)
256{
257 struct e1inp_line *line = bfd->data;
258 unsigned int ts_nr = bfd->priv_nr;
259 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
260 struct e1inp_sign_link *sign_link;
261 struct msgb *msg;
262 uint8_t proto;
263 int ret;
264
265 bfd->when &= ~BSC_FD_WRITE;
266
267 /* get the next msg for this timeslot */
268 msg = e1inp_tx_ts(e1i_ts, &sign_link);
269 if (!msg) {
270 /* no message after tx delay timer */
271 return 0;
272 }
273
274 switch (sign_link->type) {
275 case E1INP_SIGN_OML:
276 proto = IPAC_PROTO_OML;
277 break;
278 case E1INP_SIGN_RSL:
279 proto = IPAC_PROTO_RSL;
280 break;
281 default:
282 msgb_free(msg);
283 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
284 return -EINVAL;
285 }
286
287 msg->l2h = msg->data;
288 ipaccess_prepend_header(msg, sign_link->tei);
289
290 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
291
292 ret = send(bfd->fd, msg->data, msg->len, 0);
293 msgb_free(msg);
294
295 /* set tx delay timer for next event */
296 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
297 e1i_ts->sign.tx_timer.data = e1i_ts;
298
299 /* Reducing this might break the nanoBTS 900 init. */
300 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
301
302 return ret;
303}
304
305/* callback from select.c in case one of the fd's can be read/written */
306static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
307{
308 struct e1inp_line *line = bfd->data;
309 unsigned int ts_nr = bfd->priv_nr;
310 unsigned int idx = ts_nr-1;
311 struct e1inp_ts *e1i_ts;
312 int rc = 0;
313
314 /* In case of early RSL we might not yet have a line */
315
316 if (line)
317 e1i_ts = &line->ts[idx];
318
319 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
320 if (what & BSC_FD_READ)
321 rc = handle_ts1_read(bfd);
322 if (what & BSC_FD_WRITE)
323 rc = handle_ts1_write(bfd);
324 } else
325 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
326
327 return rc;
328}
329
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200330static int ipaccess_line_update(struct e1inp_line *line,
331 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200332
333struct e1inp_driver ipaccess_driver = {
334 .name = "ipa",
335 .want_write = ts_want_write,
336 .line_update = ipaccess_line_update,
337 .default_delay = 0,
338};
339
340/* callback of the OML listening filedescriptor */
341static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
342{
343 int ret;
344 int idx = 0;
345 int i;
346 struct e1inp_line *line = listen_bfd->data;
347 struct e1inp_ts *e1i_ts;
348 struct osmo_fd *bfd;
349 struct sockaddr_in sa;
350 socklen_t sa_len = sizeof(sa);
351
352 if (!(what & BSC_FD_READ))
353 return 0;
354
355 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
356 if (ret < 0) {
357 perror("accept");
358 return ret;
359 }
360 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
361 inet_ntoa(sa.sin_addr));
362
363 /* create virrtual E1 timeslots for signalling */
364 e1inp_ts_config_sign(&line->ts[1-1], line);
365
366 /* initialize the fds */
367 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
368 line->ts[i].driver.ipaccess.fd.fd = -1;
369
370 e1i_ts = &line->ts[idx];
371
372 bfd = &e1i_ts->driver.ipaccess.fd;
373 bfd->fd = ret;
374 bfd->data = line;
375 bfd->priv_nr = PRIV_OML;
376 bfd->cb = ipaccess_fd_cb;
377 bfd->when = BSC_FD_READ;
378 ret = osmo_fd_register(bfd);
379 if (ret < 0) {
380 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
381 close(bfd->fd);
382 return ret;
383 }
384
385 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
386 ret = ipaccess_send_id_req(bfd->fd);
387
388 return ret;
389}
390
391static int rsl_listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
392{
393 struct sockaddr_in sa;
394 socklen_t sa_len = sizeof(sa);
395 struct osmo_fd *bfd;
396 int ret;
397
398 if (!(what & BSC_FD_READ))
399 return 0;
400
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200401 bfd = talloc_zero(tall_ipa_ctx, struct osmo_fd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200402 if (!bfd)
403 return -ENOMEM;
404
405 /* Some BTS has connected to us, but we don't know yet which line
406 * (as created by the OML link) to associate it with. Thus, we
407 * allocate a temporary bfd until we have received ID from BTS */
408
409 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
410 if (bfd->fd < 0) {
411 perror("accept");
412 return bfd->fd;
413 }
414 LOGP(DINP, LOGL_NOTICE, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
415 bfd->priv_nr = PRIV_RSL;
416 bfd->cb = ipaccess_fd_cb;
417 bfd->when = BSC_FD_READ;
418 ret = osmo_fd_register(bfd);
419 if (ret < 0) {
420 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
421 close(bfd->fd);
422 talloc_free(bfd);
423 return ret;
424 }
425 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
426 ret = ipaccess_send_id_req(bfd->fd);
427
428 return 0;
429}
430
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200431static int ipaccess_bts_cb(struct ipa_client_link *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200432{
433 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
434 struct e1inp_ts *e1i_ts = NULL;
435 struct e1inp_sign_link *sign_link;
436
437 /* special handling for IPA CCM. */
438 if (hh->proto == IPAC_PROTO_IPACCESS) {
439 uint8_t msg_type = *(msg->l2h);
440
441 /* ping, pong and acknowledgment cases. */
442 ipaccess_rcvmsg_base(msg, &link->ofd);
443
444 /* this is a request for identification from the BSC. */
445 if (msg_type == IPAC_MSGT_ID_GET) {
446 LOGP(DINP, LOGL_NOTICE, "received ID get\n");
447 if (!link->line->ops.sign_link_up) {
448 LOGP(DINP, LOGL_ERROR, "Fix your application, "
449 "no action set if the signalling link "
450 "becomes ready\n");
451 return -EINVAL;
452 }
453 link->line->ops.sign_link_up(msg, link->line);
454 }
455 return 0;
456 } else if (link->port == IPA_TCP_PORT_OML)
457 e1i_ts = &link->line->ts[0];
458 else if (link->port == IPA_TCP_PORT_RSL)
459 e1i_ts = &link->line->ts[1];
460
461 /* look up for some existing signaling link. */
462 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
463 if (sign_link == NULL) {
464 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
465 "hh->proto=0x%02x\n", hh->proto);
466 msgb_free(msg);
467 return -EIO;
468 }
469 msg->dst = sign_link;
470
471 /* XXX better use e1inp_ts_rx? */
472 if (!link->line->ops.sign_link) {
473 LOGP(DINP, LOGL_ERROR, "Fix your application, "
474 "no action set for signalling messages.\n");
475 return -ENOENT;
476 }
477 link->line->ops.sign_link(msg, sign_link);
478 return 0;
479}
480
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200481static int ipaccess_line_update(struct e1inp_line *line,
482 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200483{
484 int ret = -ENOENT;
485
486 switch(role) {
487 case E1INP_LINE_R_BSC:
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200488 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BSC mode\n");
489
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200490 /* Listen for OML connections */
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200491 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200492 addr, IPA_TCP_PORT_OML, OSMO_SOCK_F_BIND);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200493 if (ret < 0)
494 return ret;
495
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200496 e1h->listen_fd.fd = ret;
497 e1h->listen_fd.when |= BSC_FD_READ;
498 e1h->listen_fd.cb = listen_fd_cb;
499 e1h->listen_fd.data = line;
500
501 if (osmo_fd_register(&e1h->listen_fd) < 0) {
502 close(ret);
503 return ret;
504 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200505 /* Listen for RSL connections */
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200506 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200507 addr, IPA_TCP_PORT_RSL, OSMO_SOCK_F_BIND);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508 if (ret < 0)
509 return ret;
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200510
511 e1h->rsl_listen_fd.fd = ret;
512 e1h->rsl_listen_fd.when |= BSC_FD_READ;
513 e1h->rsl_listen_fd.cb = rsl_listen_fd_cb;
514 e1h->rsl_listen_fd.data = NULL;
515
516 if (osmo_fd_register(&e1h->rsl_listen_fd) < 0) {
517 close(ret);
518 return ret;
519 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200521 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200522 struct ipa_client_link *link, *rsl_link;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200523
524 LOGP(DINP, LOGL_NOTICE, "enabling ipaccess BTS mode\n");
525
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200526 link = ipa_client_link_create(tall_ipa_ctx, line,
527 addr, IPA_TCP_PORT_OML,
528 ipaccess_bts_cb);
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200529 if (link == NULL) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200530 LOGP(DINP, LOGL_ERROR, "cannot create OML "
531 "BTS link: %s\n", strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200532 return -ENOMEM;
533 }
534 if (ipa_client_link_open(link) < 0) {
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200535 LOGP(DINP, LOGL_ERROR, "cannot open OML BTS link: %s\n",
536 strerror(errno));
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200537 ipa_client_link_close(link);
538 ipa_client_link_destroy(link);
539 return -EIO;
540 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200541 rsl_link = ipa_client_link_create(tall_ipa_ctx, line,
542 addr, IPA_TCP_PORT_RSL,
543 ipaccess_bts_cb);
Pablo Neira Ayuso29465d32011-06-21 14:21:33 +0200544 if (rsl_link == NULL) {
545 LOGP(DINP, LOGL_ERROR, "cannot create RSL "
546 "BTS link: %s\n", strerror(errno));
547 return -ENOMEM;
548 }
549 if (ipa_client_link_open(rsl_link) < 0) {
550 LOGP(DINP, LOGL_ERROR, "cannot open RSL BTS link: %s\n",
551 strerror(errno));
552 ipa_client_link_close(rsl_link);
553 ipa_client_link_destroy(rsl_link);
554 return -EIO;
555 }
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200556 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200557 break;
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +0200558 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200559 default:
560 break;
561 }
562 return ret;
563}
564
565/* Actively connect to a BTS. Currently used by ipaccess-config.c */
566int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
567{
568 struct e1inp_ts *e1i_ts = &line->ts[0];
569 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
570 int ret, on = 1;
571
572 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
573 bfd->cb = ipaccess_fd_cb;
574 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
575 bfd->data = line;
576 bfd->priv_nr = PRIV_OML;
577
578 if (bfd->fd < 0) {
579 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
580 return -EIO;
581 }
582
583 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
584
585 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
586 if (ret < 0) {
587 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
588 close(bfd->fd);
589 return ret;
590 }
591
592 ret = osmo_fd_register(bfd);
593 if (ret < 0) {
594 close(bfd->fd);
595 return ret;
596 }
597
598 line->driver = &ipaccess_driver;
599
600 return ret;
601 //return e1inp_line_register(line);
602}
603
604int ipaccess_setup(struct gsm_network *gsmnet)
605{
606 e1h->gsmnet = gsmnet;
607 return 0;
608}
609
610void e1inp_ipaccess_init(void)
611{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200612 tall_ipa_ctx = talloc_named_const(libosmo_abis_ctx, 1, "ipa");
613
614 e1h = talloc_zero(tall_ipa_ctx, struct ia_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200615 if (!e1h)
616 return;
617
618 e1inp_driver_register(&ipaccess_driver);
619}