blob: 74c850c9fa5150eb48ab663b3e7840712cc0008c [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>
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +02004 * (C) 2010 by Holger Hans Peter Freyther
5 * (C) 2010 by On-Waves
Harald Welte5fd8a542009-02-13 02:43:36 +00006 *
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 General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <time.h>
31#include <sys/fcntl.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <arpa/inet.h>
36
Harald Weltedfe6c7d2010-02-20 16:24:02 +010037#include <osmocore/select.h>
38#include <osmocore/tlv.h>
39#include <osmocore/msgb.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000040#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
42#include <openbsc/abis_nm.h>
43#include <openbsc/abis_rsl.h>
44#include <openbsc/subchan_demux.h>
45#include <openbsc/e1_input.h>
Harald Welte4f361fc2009-02-15 15:32:53 +000046#include <openbsc/ipaccess.h>
Harald Welte5540c4c2010-05-19 14:38:50 +020047#include <openbsc/socket.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010048#include <osmocore/talloc.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000049
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +010050#define PRIV_OML 1
51#define PRIV_RSL 2
52
Harald Welte5fd8a542009-02-13 02:43:36 +000053/* data structure for one E1 interface with A-bis */
54struct ia_e1_handle {
55 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000056 struct bsc_fd rsl_listen_fd;
Harald Welteedb37782009-05-01 14:59:07 +000057 struct gsm_network *gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +000058};
59
Harald Welteedb37782009-05-01 14:59:07 +000060static struct ia_e1_handle *e1h;
61
62
Holger Hans Peter Freyther6c8c0dd2010-04-04 18:12:37 +020063#define TS1_ALLOC_SIZE 900
Harald Welte5fd8a542009-02-13 02:43:36 +000064
Harald Welte4f361fc2009-02-15 15:32:53 +000065static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
66static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
Harald Welteedb37782009-05-01 14:59:07 +000067static const u_int8_t id_req[] = { 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020068 0x01, IPAC_IDTAG_UNIT,
Harald Welteedb37782009-05-01 14:59:07 +000069 0x01, IPAC_IDTAG_MACADDR,
70 0x01, IPAC_IDTAG_LOCATION1,
71 0x01, IPAC_IDTAG_LOCATION2,
72 0x01, IPAC_IDTAG_EQUIPVERS,
73 0x01, IPAC_IDTAG_SWVERSION,
74 0x01, IPAC_IDTAG_UNITNAME,
75 0x01, IPAC_IDTAG_SERNR,
76 };
Harald Welte5fd8a542009-02-13 02:43:36 +000077
Harald Welteedb37782009-05-01 14:59:07 +000078static const char *idtag_names[] = {
79 [IPAC_IDTAG_SERNR] = "Serial_Number",
80 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
81 [IPAC_IDTAG_LOCATION1] = "Location_1",
82 [IPAC_IDTAG_LOCATION2] = "Location_2",
83 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
84 [IPAC_IDTAG_SWVERSION] = "Software_Version",
85 [IPAC_IDTAG_IPADDR] = "IP_Address",
86 [IPAC_IDTAG_MACADDR] = "MAC_Address",
87 [IPAC_IDTAG_UNIT] = "Unit_ID",
88};
89
90static const char *ipac_idtag_name(int tag)
Harald Welte5fd8a542009-02-13 02:43:36 +000091{
Harald Welteedb37782009-05-01 14:59:07 +000092 if (tag >= ARRAY_SIZE(idtag_names))
93 return "unknown";
94
95 return idtag_names[tag];
96}
97
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +010098int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welteedb37782009-05-01 14:59:07 +000099{
100 u_int8_t t_len;
101 u_int8_t t_tag;
102 u_int8_t *cur = buf;
103
Holger Hans Peter Freyther949e0ba2010-10-14 17:21:04 +0200104 memset(dec, 0, sizeof(*dec));
105
Harald Welteedb37782009-05-01 14:59:07 +0000106 while (cur < buf + len) {
107 t_len = *cur++;
108 t_tag = *cur++;
109
110 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
111
112 dec->lv[t_tag].len = t_len;
113 dec->lv[t_tag].val = cur;
114
115 cur += t_len;
116 }
117 return 0;
118}
119
120struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
121 u_int16_t site_id, u_int16_t bts_id)
122{
Harald Weltee441d9c2009-06-21 16:17:15 +0200123 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000124
Harald Weltee441d9c2009-06-21 16:17:15 +0200125 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000126
127 if (!is_ipaccess_bts(bts))
128 continue;
129
130 if (bts->ip_access.site_id == site_id &&
131 bts->ip_access.bts_id == bts_id)
132 return bts;
133 }
134
135 return NULL;
136}
137
138static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
139 u_int16_t *trx_id)
140{
141 unsigned long ul;
142 char *endptr;
143 const char *nptr;
144
145 nptr = str;
146 ul = strtoul(nptr, &endptr, 10);
147 if (endptr <= nptr)
148 return -EINVAL;
149 if (site_id)
150 *site_id = ul & 0xffff;
151
152 if (*endptr++ != '/')
153 return -EINVAL;
154
155 nptr = endptr;
156 ul = strtoul(nptr, &endptr, 10);
157 if (endptr <= nptr)
158 return -EINVAL;
159 if (bts_id)
160 *bts_id = ul & 0xffff;
161
162 if (*endptr++ != '/')
163 return -EINVAL;
164
165 nptr = endptr;
166 ul = strtoul(nptr, &endptr, 10);
167 if (endptr <= nptr)
168 return -EINVAL;
169 if (trx_id)
170 *trx_id = ul & 0xffff;
171
172 return 0;
173}
174
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100175/* send the id ack */
176int ipaccess_send_id_ack(int fd)
177{
178 return write(fd, id_ack, sizeof(id_ack));
179}
180
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100181int ipaccess_send_id_req(int fd)
182{
183 return write(fd, id_req, sizeof(id_req));
184}
185
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200186/* base handling of the ip.access protocol */
187int ipaccess_rcvmsg_base(struct msgb *msg,
188 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000189{
Harald Welte5fd8a542009-02-13 02:43:36 +0000190 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000191 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000192
Harald Welte5fd8a542009-02-13 02:43:36 +0000193 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000194 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000195 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000196 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000197 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000198 DEBUGP(DMI, "PONG!\n");
199 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200200 case IPAC_MSGT_ID_ACK:
201 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100202 ret = ipaccess_send_id_ack(bfd->fd);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200203 break;
204 }
205 return 0;
206}
207
208static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
209 struct bsc_fd *bfd)
210{
211 struct tlv_parsed tlvp;
212 u_int8_t msg_type = *(msg->l2h);
213 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
214 struct gsm_bts *bts;
215
216 /* handle base messages */
217 ipaccess_rcvmsg_base(msg, bfd);
218
219 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000220 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000221 DEBUGP(DMI, "ID_RESP ");
222 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100223 ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
Harald Welteedb37782009-05-01 14:59:07 +0000224 msgb_l2len(msg)-2);
225 DEBUGP(DMI, "\n");
226
227 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
228 break;
229
230 /* lookup BTS, create sign_link, ... */
231 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
232 &site_id, &bts_id, &trx_id);
233 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
234 if (!bts) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100235 LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for "
Harald Welteedb37782009-05-01 14:59:07 +0000236 " %u/%u/%u, disconnecting\n", site_id, bts_id,
237 trx_id);
238 return -EIO;
239 }
240 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100241 if (bfd->priv_nr == PRIV_OML) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200242 /* drop any old oml connection */
243 ipaccess_drop_oml(bts);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100244 bts->oml_link = e1inp_sign_link_create(&line->ts[PRIV_OML - 1],
Harald Welteedb37782009-05-01 14:59:07 +0000245 E1INP_SIGN_OML, bts->c0,
Harald Welte8175e952009-10-20 00:22:00 +0200246 bts->oml_tei, 0);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100247 } else if (bfd->priv_nr == PRIV_RSL) {
Harald Welteedb37782009-05-01 14:59:07 +0000248 struct e1inp_ts *e1i_ts;
249 struct bsc_fd *newbfd;
Harald Welte8175e952009-10-20 00:22:00 +0200250 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200251
252 /* drop any old rsl connection */
253 ipaccess_drop_rsl(trx);
254
255 if (!bts->oml_link) {
256 bsc_unregister_fd(bfd);
257 close(bfd->fd);
258 bfd->fd = -1;
259 talloc_free(bfd);
260 return 0;
261 }
262
Harald Welteedb37782009-05-01 14:59:07 +0000263 bfd->data = line = bts->oml_link->ts->line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100264 e1i_ts = &line->ts[PRIV_RSL + trx_id - 1];
Harald Welteedb37782009-05-01 14:59:07 +0000265 newbfd = &e1i_ts->driver.ipaccess.fd;
Harald Welte8175e952009-10-20 00:22:00 +0200266 e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000267
Harald Welte8175e952009-10-20 00:22:00 +0200268 trx->rsl_link = e1inp_sign_link_create(e1i_ts,
269 E1INP_SIGN_RSL, trx,
270 trx->rsl_tei, 0);
Holger Hans Peter Freyther354ef812010-03-24 04:02:55 +0100271
Harald Welteedb37782009-05-01 14:59:07 +0000272 /* get rid of our old temporary bfd */
273 memcpy(newbfd, bfd, sizeof(*newbfd));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100274 newbfd->priv_nr = PRIV_RSL + trx_id;
Harald Welteedb37782009-05-01 14:59:07 +0000275 bsc_unregister_fd(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200276 bfd->fd = -1;
Harald Weltea4ffea92009-06-22 01:36:25 +0200277 talloc_free(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200278 bsc_register_fd(newbfd);
Harald Welteedb37782009-05-01 14:59:07 +0000279 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000280 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000281 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000282 return 0;
283}
284
Harald Welte88a412a2009-12-16 17:32:37 +0100285#define OML_UP 0x0001
286#define RSL_UP 0x0002
Harald Welte7782c142009-02-15 03:39:51 +0000287
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200288/*
289 * read one ipa message from the socket
290 * return NULL in case of error
291 */
292struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000293{
Harald Welte966636f2009-06-26 19:39:35 +0200294 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000295 struct ipaccess_head *hh;
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100296 int len, ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000297
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200298 if (!msg) {
299 *error = -ENOMEM;
300 return NULL;
301 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000302
303 /* first read our 3-byte header */
304 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100305 ret = recv(bfd->fd, msg->data, sizeof(*hh), 0);
306 if (ret == 0) {
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200307 msgb_free(msg);
308 *error = ret;
309 return NULL;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100310 } else if (ret != sizeof(*hh)) {
311 if (errno != EAGAIN)
312 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200313 msgb_free(msg);
314 *error = ret;
315 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000316 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200317
Harald Welte5fd8a542009-02-13 02:43:36 +0000318 msgb_put(msg, ret);
319
320 /* then read te length as specified in header */
321 msg->l2h = msg->data + sizeof(*hh);
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100322 len = ntohs(hh->len);
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100323
324 if (len < 0 || TS1_ALLOC_SIZE < len + sizeof(*hh)) {
325 LOGP(DINP, LOGL_ERROR, "Can not read this packet. %d avail\n", len);
326 msgb_free(msg);
327 *error = -EIO;
328 return NULL;
329 }
330
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100331 ret = recv(bfd->fd, msg->l2h, len, 0);
332 if (ret < len) {
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100333 LOGP(DINP, LOGL_ERROR, "short read! Got %d from %d\n", ret, len);
Harald Welte5c1e4582009-02-15 11:57:29 +0000334 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200335 *error = -EIO;
336 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000337 }
338 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200339
340 return msg;
341}
342
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200343int ipaccess_drop_oml(struct gsm_bts *bts)
344{
345 struct gsm_bts_trx *trx;
346 struct e1inp_ts *ts;
347 struct e1inp_line *line;
348 struct bsc_fd *bfd;
349
350 if (!bts || !bts->oml_link)
351 return -1;
352
353 /* send OML down */
354 ts = bts->oml_link->ts;
355 line = ts->line;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200356 e1inp_event(ts, EVT_E1_TEI_DN, bts->oml_link->tei, bts->oml_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200357
358 bfd = &ts->driver.ipaccess.fd;
359 bsc_unregister_fd(bfd);
360 close(bfd->fd);
361 bfd->fd = -1;
362
363 /* clean up OML and RSL */
364 e1inp_sign_link_destroy(bts->oml_link);
365 bts->oml_link = NULL;
366 bts->ip_access.flags = 0;
367
368 /* drop all RSL connections too */
369 llist_for_each_entry(trx, &bts->trx_list, list)
370 ipaccess_drop_rsl(trx);
371
372 /* kill the E1 line now... as we have no one left to use it */
373 talloc_free(line);
374
375 return -1;
376}
377
378static int ipaccess_drop(struct e1inp_ts *ts, struct bsc_fd *bfd)
379{
380 struct e1inp_sign_link *link;
381 int bts_nr;
382
383 if (!ts) {
384 /*
385 * If we don't have a TS this means that this is a RSL
386 * connection but we are not past the authentication
387 * handling yet. So we can safely delete this bfd and
388 * wait for a reconnect.
389 */
390 bsc_unregister_fd(bfd);
391 close(bfd->fd);
392 bfd->fd = -1;
393 talloc_free(bfd);
394 return -1;
395 }
396
397 /* attempt to find a signalling link */
398 if (ts->type == E1INP_TS_TYPE_SIGN) {
399 llist_for_each_entry(link, &ts->sign.sign_links, list) {
400 bts_nr = link->trx->bts->bts_nr;
401 /* we have issues just reconnecting RLS so we drop OML */
402 ipaccess_drop_oml(link->trx->bts);
403 return bts_nr;
404 }
405 }
406
407 /* error case */
408 LOGP(DINP, LOGL_ERROR, "Failed to find a signalling link for ts: %p\n", ts);
409 bsc_unregister_fd(bfd);
410 close(bfd->fd);
411 bfd->fd = -1;
412 return -1;
413}
414
415int ipaccess_drop_rsl(struct gsm_bts_trx *trx)
416{
417 struct bsc_fd *bfd;
418 struct e1inp_ts *ts;
419
420 if (!trx || !trx->rsl_link)
421 return -1;
422
423 /* send RSL down */
424 ts = trx->rsl_link->ts;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200425 e1inp_event(ts, EVT_E1_TEI_DN, trx->rsl_link->tei, trx->rsl_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200426
427 /* close the socket */
428 bfd = &ts->driver.ipaccess.fd;
429 bsc_unregister_fd(bfd);
430 close(bfd->fd);
431 bfd->fd = -1;
432
433 /* destroy */
434 e1inp_sign_link_destroy(trx->rsl_link);
435 trx->rsl_link = NULL;
436
437 return -1;
438}
439
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200440static int handle_ts1_read(struct bsc_fd *bfd)
441{
442 struct e1inp_line *line = bfd->data;
443 unsigned int ts_nr = bfd->priv_nr;
444 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
445 struct e1inp_sign_link *link;
446 struct msgb *msg;
447 struct ipaccess_head *hh;
Holger Hans Peter Freyther67b59612009-10-27 10:08:38 +0100448 int ret = 0, error;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200449
450 msg = ipaccess_read_msg(bfd, &error);
451 if (!msg) {
452 if (error == 0) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200453 int ret = ipaccess_drop(e1i_ts, bfd);
454 if (ret >= 0)
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100455 LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n",
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200456 ret);
457 else
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100458 LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n");
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200459 }
460 return error;
461 }
462
Sylvain Munaut86538c72009-09-30 22:35:04 +0200463 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000464
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200465 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000466 if (hh->proto == IPAC_PROTO_IPACCESS) {
467 ret = ipaccess_rcvmsg(line, msg, bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200468 if (ret < 0)
469 ipaccess_drop(e1i_ts, bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000470 msgb_free(msg);
471 return ret;
472 }
473 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
474 * might have free'd it !!! */
475
Harald Welte8175e952009-10-20 00:22:00 +0200476 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
Harald Welte5fd8a542009-02-13 02:43:36 +0000477 if (!link) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100478 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
479 "hh->proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000480 msgb_free(msg);
481 return -EIO;
482 }
483 msg->trx = link->trx;
484
Harald Welte8175e952009-10-20 00:22:00 +0200485 switch (link->type) {
486 case E1INP_SIGN_RSL:
Harald Welte1394fea2009-12-21 23:01:33 +0100487 if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) {
Harald Weltee73501a2009-10-21 21:16:00 +0200488 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte1394fea2009-12-21 23:01:33 +0100489 msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr);
Harald Weltee73501a2009-10-21 21:16:00 +0200490 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000491 ret = abis_rsl_rcvmsg(msg);
492 break;
Harald Welte8175e952009-10-20 00:22:00 +0200493 case E1INP_SIGN_OML:
Harald Welte88a412a2009-12-16 17:32:37 +0100494 if (!(msg->trx->bts->ip_access.flags & OML_UP)) {
Harald Welte8175e952009-10-20 00:22:00 +0200495 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte88a412a2009-12-16 17:32:37 +0100496 msg->trx->bts->ip_access.flags |= OML_UP;
Harald Welte7782c142009-02-15 03:39:51 +0000497 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000498 ret = abis_nm_rcvmsg(msg);
499 break;
500 default:
Harald Welteafdca0f2009-12-23 23:01:04 +0100501 LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000502 msgb_free(msg);
503 break;
504 }
505 return ret;
506}
507
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200508void ipaccess_prepend_header(struct msgb *msg, int proto)
509{
510 struct ipaccess_head *hh;
511
512 /* prepend the ip.access header */
513 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100514 hh->len = htons(msg->len - sizeof(*hh));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200515 hh->proto = proto;
516}
517
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200518static int ts_want_write(struct e1inp_ts *e1i_ts)
519{
520 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
521
522 return 0;
523}
524
525static void timeout_ts1_write(void *data)
526{
527 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
528
529 /* trigger write of ts1, due to tx delay timer */
530 ts_want_write(e1i_ts);
531}
532
Harald Welte5fd8a542009-02-13 02:43:36 +0000533static int handle_ts1_write(struct bsc_fd *bfd)
534{
535 struct e1inp_line *line = bfd->data;
536 unsigned int ts_nr = bfd->priv_nr;
537 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
538 struct e1inp_sign_link *sign_link;
539 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200540 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000541 int ret;
542
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200543 bfd->when &= ~BSC_FD_WRITE;
544
Harald Welte5fd8a542009-02-13 02:43:36 +0000545 /* get the next msg for this timeslot */
546 msg = e1inp_tx_ts(e1i_ts, &sign_link);
547 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200548 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000549 return 0;
550 }
551
Harald Welte5fd8a542009-02-13 02:43:36 +0000552 switch (sign_link->type) {
553 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200554 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000555 break;
556 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200557 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000558 break;
559 default:
560 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200561 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000562 return -EINVAL;
563 }
564
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200565 msg->l2h = msg->data;
Harald Welte8175e952009-10-20 00:22:00 +0200566 ipaccess_prepend_header(msg, sign_link->tei);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200567
568 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000569
570 ret = send(bfd->fd, msg->data, msg->len, 0);
571 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200572
573 /* set tx delay timer for next event */
574 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
575 e1i_ts->sign.tx_timer.data = e1i_ts;
Holger Hans Peter Freyther63cb4472010-04-11 10:10:04 +0200576
577 /* Reducing this might break the nanoBTS 900 init. */
578 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000579
580 return ret;
581}
582
Harald Welte5fd8a542009-02-13 02:43:36 +0000583/* callback from select.c in case one of the fd's can be read/written */
584static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
585{
586 struct e1inp_line *line = bfd->data;
587 unsigned int ts_nr = bfd->priv_nr;
588 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000589 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000590 int rc = 0;
591
Harald Welteedb37782009-05-01 14:59:07 +0000592 /* In case of early RSL we might not yet have a line */
593
594 if (line)
595 e1i_ts = &line->ts[idx];
596
597 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000598 if (what & BSC_FD_READ)
599 rc = handle_ts1_read(bfd);
600 if (what & BSC_FD_WRITE)
601 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000602 } else
Harald Welteafdca0f2009-12-23 23:01:04 +0100603 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000604
605 return rc;
606}
607
Harald Welte5fd8a542009-02-13 02:43:36 +0000608struct e1inp_driver ipaccess_driver = {
609 .name = "ip.access",
610 .want_write = ts_want_write,
611};
612
Harald Welteedb37782009-05-01 14:59:07 +0000613/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000614static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
615{
Harald Welte5fd8a542009-02-13 02:43:36 +0000616 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000617 int idx = 0;
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100618 int i;
Harald Welteedb37782009-05-01 14:59:07 +0000619 struct e1inp_line *line;
620 struct e1inp_ts *e1i_ts;
621 struct bsc_fd *bfd;
622 struct sockaddr_in sa;
623 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000624
Harald Welteedb37782009-05-01 14:59:07 +0000625 if (!(what & BSC_FD_READ))
626 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000627
Harald Welteedb37782009-05-01 14:59:07 +0000628 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
629 if (ret < 0) {
630 perror("accept");
631 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000632 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100633 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
634 inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000635
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100636 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000637 if (!line) {
638 close(ret);
639 return -ENOMEM;
640 }
Harald Welteedb37782009-05-01 14:59:07 +0000641 line->driver = &ipaccess_driver;
642 //line->driver_data = e1h;
643 /* create virrtual E1 timeslots for signalling */
644 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000645
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100646 /* initialize the fds */
647 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
648 line->ts[i].driver.ipaccess.fd.fd = -1;
649
Harald Welteedb37782009-05-01 14:59:07 +0000650 e1i_ts = &line->ts[idx];
651
652 bfd = &e1i_ts->driver.ipaccess.fd;
653 bfd->fd = ret;
654 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100655 bfd->priv_nr = PRIV_OML;
Harald Welteedb37782009-05-01 14:59:07 +0000656 bfd->cb = ipaccess_fd_cb;
657 bfd->when = BSC_FD_READ;
658 ret = bsc_register_fd(bfd);
659 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100660 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000661 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200662 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000663 return ret;
664 }
665
666 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100667 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000668
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200669 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200670 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000671}
672
Harald Welte5c1e4582009-02-15 11:57:29 +0000673static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
674{
Harald Welteedb37782009-05-01 14:59:07 +0000675 struct sockaddr_in sa;
676 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200677 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000678 int ret;
679
Harald Welteedb37782009-05-01 14:59:07 +0000680 if (!(what & BSC_FD_READ))
681 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000682
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100683 bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200684 if (!bfd)
685 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200686
Harald Welteedb37782009-05-01 14:59:07 +0000687 /* Some BTS has connected to us, but we don't know yet which line
688 * (as created by the OML link) to associate it with. Thus, we
Holger Hans Peter Freytherc7df7c62009-11-15 13:50:39 +0100689 * allocate a temporary bfd until we have received ID from BTS */
Harald Welteedb37782009-05-01 14:59:07 +0000690
691 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
692 if (bfd->fd < 0) {
693 perror("accept");
694 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000695 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100696 LOGP(DINP, LOGL_NOTICE, "accept()ed new RSL link from %s\n", inet_ntoa(sa.sin_addr));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100697 bfd->priv_nr = PRIV_RSL;
Harald Welteedb37782009-05-01 14:59:07 +0000698 bfd->cb = ipaccess_fd_cb;
699 bfd->when = BSC_FD_READ;
700 ret = bsc_register_fd(bfd);
701 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100702 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000703 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200704 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000705 return ret;
706 }
707 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
708 ret = write(bfd->fd, id_req, sizeof(id_req));
709
Harald Welte5c1e4582009-02-15 11:57:29 +0000710 return 0;
711}
712
Harald Welte25de9912009-04-30 15:53:07 +0000713/* Actively connect to a BTS. Currently used by ipaccess-config.c */
714int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
715{
716 struct e1inp_ts *e1i_ts = &line->ts[0];
717 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
718 int ret, on = 1;
719
720 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
721 bfd->cb = ipaccess_fd_cb;
722 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
723 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100724 bfd->priv_nr = PRIV_OML;
Harald Welte25de9912009-04-30 15:53:07 +0000725
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100726 if (bfd->fd < 0) {
727 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
728 return -EIO;
729 }
730
Harald Welte25de9912009-04-30 15:53:07 +0000731 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
732
733 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
734 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100735 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
Harald Welte25de9912009-04-30 15:53:07 +0000736 close(bfd->fd);
737 return ret;
738 }
739
740 ret = bsc_register_fd(bfd);
741 if (ret < 0) {
742 close(bfd->fd);
743 return ret;
744 }
745
746 line->driver = &ipaccess_driver;
747
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200748 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200749 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000750}
751
Harald Welteedb37782009-05-01 14:59:07 +0000752int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000753{
Harald Welte5c1e4582009-02-15 11:57:29 +0000754 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000755
756 /* register the driver with the core */
757 /* FIXME: do this in the plugin initializer function */
758 ret = e1inp_driver_register(&ipaccess_driver);
759 if (ret)
760 return ret;
761
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100762 e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200763 if (!e1h)
764 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200765
Harald Welteedb37782009-05-01 14:59:07 +0000766 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000767
Harald Welte5c1e4582009-02-15 11:57:29 +0000768 /* Listen for OML connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200769 ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, 0, IPA_TCP_PORT_OML,
Harald Welte9b455bf2010-03-14 15:45:01 +0800770 listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000771 if (ret < 0)
772 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000773
Harald Welte5c1e4582009-02-15 11:57:29 +0000774 /* Listen for RSL connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200775 ret = make_sock(&e1h->rsl_listen_fd, IPPROTO_TCP, 0,
Harald Welte9b455bf2010-03-14 15:45:01 +0800776 IPA_TCP_PORT_RSL, rsl_listen_fd_cb);
777 if (ret < 0)
778 return ret;
779
Harald Welteedb37782009-05-01 14:59:07 +0000780 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000781}