blob: e7098dcaf50dc01eb448ebcac3c2f0c0bfdfbfb6 [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
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * 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
Harald Welte5fd8a542009-02-13 02:43:36 +000012 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte5fd8a542009-02-13 02:43:36 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * 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/>.
Harald Welte5fd8a542009-02-13 02:43:36 +000021 *
22 */
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <string.h>
29#include <time.h>
30#include <sys/fcntl.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35
Harald Weltedfe6c7d2010-02-20 16:24:02 +010036#include <osmocore/select.h>
37#include <osmocore/tlv.h>
38#include <osmocore/msgb.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
41#include <openbsc/abis_nm.h>
42#include <openbsc/abis_rsl.h>
43#include <openbsc/subchan_demux.h>
44#include <openbsc/e1_input.h>
Harald Welte4f361fc2009-02-15 15:32:53 +000045#include <openbsc/ipaccess.h>
Harald Welte5540c4c2010-05-19 14:38:50 +020046#include <openbsc/socket.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010047#include <osmocore/talloc.h>
Harald Welte5fd8a542009-02-13 02:43:36 +000048
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +010049#define PRIV_OML 1
50#define PRIV_RSL 2
51
Harald Welte5fd8a542009-02-13 02:43:36 +000052/* data structure for one E1 interface with A-bis */
53struct ia_e1_handle {
54 struct bsc_fd listen_fd;
Harald Welte5c1e4582009-02-15 11:57:29 +000055 struct bsc_fd rsl_listen_fd;
Harald Welteedb37782009-05-01 14:59:07 +000056 struct gsm_network *gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +000057};
58
Harald Welteedb37782009-05-01 14:59:07 +000059static struct ia_e1_handle *e1h;
60
61
Holger Hans Peter Freyther6c8c0dd2010-04-04 18:12:37 +020062#define TS1_ALLOC_SIZE 900
Harald Welte5fd8a542009-02-13 02:43:36 +000063
Harald Welte4f361fc2009-02-15 15:32:53 +000064static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
65static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
Harald Welteedb37782009-05-01 14:59:07 +000066static const u_int8_t id_req[] = { 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET,
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020067 0x01, IPAC_IDTAG_UNIT,
Harald Welteedb37782009-05-01 14:59:07 +000068 0x01, IPAC_IDTAG_MACADDR,
69 0x01, IPAC_IDTAG_LOCATION1,
70 0x01, IPAC_IDTAG_LOCATION2,
71 0x01, IPAC_IDTAG_EQUIPVERS,
72 0x01, IPAC_IDTAG_SWVERSION,
73 0x01, IPAC_IDTAG_UNITNAME,
74 0x01, IPAC_IDTAG_SERNR,
75 };
Harald Welte5fd8a542009-02-13 02:43:36 +000076
Harald Welteedb37782009-05-01 14:59:07 +000077static const char *idtag_names[] = {
78 [IPAC_IDTAG_SERNR] = "Serial_Number",
79 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
80 [IPAC_IDTAG_LOCATION1] = "Location_1",
81 [IPAC_IDTAG_LOCATION2] = "Location_2",
82 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
83 [IPAC_IDTAG_SWVERSION] = "Software_Version",
84 [IPAC_IDTAG_IPADDR] = "IP_Address",
85 [IPAC_IDTAG_MACADDR] = "MAC_Address",
86 [IPAC_IDTAG_UNIT] = "Unit_ID",
87};
88
89static const char *ipac_idtag_name(int tag)
Harald Welte5fd8a542009-02-13 02:43:36 +000090{
Harald Welteedb37782009-05-01 14:59:07 +000091 if (tag >= ARRAY_SIZE(idtag_names))
92 return "unknown";
93
94 return idtag_names[tag];
95}
96
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +010097int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welteedb37782009-05-01 14:59:07 +000098{
99 u_int8_t t_len;
100 u_int8_t t_tag;
101 u_int8_t *cur = buf;
102
Holger Hans Peter Freyther949e0ba2010-10-14 17:21:04 +0200103 memset(dec, 0, sizeof(*dec));
104
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200105 while (len >= 2) {
106 len -= 2;
Harald Welteedb37782009-05-01 14:59:07 +0000107 t_len = *cur++;
108 t_tag = *cur++;
109
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200110 if (t_len > len + 1) {
111 LOGP(DMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
112 return -1;
113 }
114
Harald Welteedb37782009-05-01 14:59:07 +0000115 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
116
117 dec->lv[t_tag].len = t_len;
118 dec->lv[t_tag].val = cur;
119
120 cur += t_len;
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200121 len -= t_len;
Harald Welteedb37782009-05-01 14:59:07 +0000122 }
123 return 0;
124}
125
126struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
127 u_int16_t site_id, u_int16_t bts_id)
128{
Harald Weltee441d9c2009-06-21 16:17:15 +0200129 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000130
Harald Weltee441d9c2009-06-21 16:17:15 +0200131 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000132
133 if (!is_ipaccess_bts(bts))
134 continue;
135
136 if (bts->ip_access.site_id == site_id &&
137 bts->ip_access.bts_id == bts_id)
138 return bts;
139 }
140
141 return NULL;
142}
143
144static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
145 u_int16_t *trx_id)
146{
147 unsigned long ul;
148 char *endptr;
149 const char *nptr;
150
151 nptr = str;
152 ul = strtoul(nptr, &endptr, 10);
153 if (endptr <= nptr)
154 return -EINVAL;
155 if (site_id)
156 *site_id = ul & 0xffff;
157
158 if (*endptr++ != '/')
159 return -EINVAL;
160
161 nptr = endptr;
162 ul = strtoul(nptr, &endptr, 10);
163 if (endptr <= nptr)
164 return -EINVAL;
165 if (bts_id)
166 *bts_id = ul & 0xffff;
167
168 if (*endptr++ != '/')
169 return -EINVAL;
170
171 nptr = endptr;
172 ul = strtoul(nptr, &endptr, 10);
173 if (endptr <= nptr)
174 return -EINVAL;
175 if (trx_id)
176 *trx_id = ul & 0xffff;
177
178 return 0;
179}
180
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100181/* send the id ack */
182int ipaccess_send_id_ack(int fd)
183{
184 return write(fd, id_ack, sizeof(id_ack));
185}
186
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100187int ipaccess_send_id_req(int fd)
188{
189 return write(fd, id_req, sizeof(id_req));
190}
191
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200192/* base handling of the ip.access protocol */
193int ipaccess_rcvmsg_base(struct msgb *msg,
194 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000195{
Harald Welte5fd8a542009-02-13 02:43:36 +0000196 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000197 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000198
Harald Welte5fd8a542009-02-13 02:43:36 +0000199 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000200 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000201 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000202 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000203 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000204 DEBUGP(DMI, "PONG!\n");
205 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200206 case IPAC_MSGT_ID_ACK:
207 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100208 ret = ipaccess_send_id_ack(bfd->fd);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200209 break;
210 }
211 return 0;
212}
213
214static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
215 struct bsc_fd *bfd)
216{
217 struct tlv_parsed tlvp;
218 u_int8_t msg_type = *(msg->l2h);
219 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
220 struct gsm_bts *bts;
Holger Hans Peter Freythere3839802010-10-14 21:17:30 +0200221 char *unitid;
222 int len;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200223
224 /* handle base messages */
225 ipaccess_rcvmsg_base(msg, bfd);
226
227 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000228 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000229 DEBUGP(DMI, "ID_RESP ");
230 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100231 ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
Harald Welteedb37782009-05-01 14:59:07 +0000232 msgb_l2len(msg)-2);
233 DEBUGP(DMI, "\n");
234
235 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
236 break;
237
Holger Hans Peter Freythere3839802010-10-14 21:17:30 +0200238 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
239 if (len < 1)
240 break;
241
Harald Welteedb37782009-05-01 14:59:07 +0000242 /* lookup BTS, create sign_link, ... */
Holger Hans Peter Freythere3839802010-10-14 21:17:30 +0200243 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
244 unitid[len - 1] = '\0';
245 parse_unitid(unitid, &site_id, &bts_id, &trx_id);
Harald Welteedb37782009-05-01 14:59:07 +0000246 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
247 if (!bts) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100248 LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for "
Harald Welteedb37782009-05-01 14:59:07 +0000249 " %u/%u/%u, disconnecting\n", site_id, bts_id,
250 trx_id);
251 return -EIO;
252 }
253 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100254 if (bfd->priv_nr == PRIV_OML) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200255 /* drop any old oml connection */
256 ipaccess_drop_oml(bts);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100257 bts->oml_link = e1inp_sign_link_create(&line->ts[PRIV_OML - 1],
Harald Welteedb37782009-05-01 14:59:07 +0000258 E1INP_SIGN_OML, bts->c0,
Harald Welte8175e952009-10-20 00:22:00 +0200259 bts->oml_tei, 0);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100260 } else if (bfd->priv_nr == PRIV_RSL) {
Harald Welteedb37782009-05-01 14:59:07 +0000261 struct e1inp_ts *e1i_ts;
262 struct bsc_fd *newbfd;
Harald Welte8175e952009-10-20 00:22:00 +0200263 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200264
265 /* drop any old rsl connection */
266 ipaccess_drop_rsl(trx);
267
268 if (!bts->oml_link) {
269 bsc_unregister_fd(bfd);
270 close(bfd->fd);
271 bfd->fd = -1;
272 talloc_free(bfd);
273 return 0;
274 }
275
Harald Welteedb37782009-05-01 14:59:07 +0000276 bfd->data = line = bts->oml_link->ts->line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100277 e1i_ts = &line->ts[PRIV_RSL + trx_id - 1];
Harald Welteedb37782009-05-01 14:59:07 +0000278 newbfd = &e1i_ts->driver.ipaccess.fd;
Harald Welte8175e952009-10-20 00:22:00 +0200279 e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000280
Harald Welte8175e952009-10-20 00:22:00 +0200281 trx->rsl_link = e1inp_sign_link_create(e1i_ts,
282 E1INP_SIGN_RSL, trx,
283 trx->rsl_tei, 0);
Holger Hans Peter Freytherd49fc5a2010-11-15 20:36:21 +0100284 trx->rsl_link->ts->sign.delay = 0;
Holger Hans Peter Freyther354ef812010-03-24 04:02:55 +0100285
Harald Welteedb37782009-05-01 14:59:07 +0000286 /* get rid of our old temporary bfd */
287 memcpy(newbfd, bfd, sizeof(*newbfd));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100288 newbfd->priv_nr = PRIV_RSL + trx_id;
Harald Welteedb37782009-05-01 14:59:07 +0000289 bsc_unregister_fd(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200290 bfd->fd = -1;
Harald Weltea4ffea92009-06-22 01:36:25 +0200291 talloc_free(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200292 bsc_register_fd(newbfd);
Harald Welteedb37782009-05-01 14:59:07 +0000293 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000294 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000295 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000296 return 0;
297}
298
Harald Welte88a412a2009-12-16 17:32:37 +0100299#define OML_UP 0x0001
300#define RSL_UP 0x0002
Harald Welte7782c142009-02-15 03:39:51 +0000301
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200302/*
303 * read one ipa message from the socket
304 * return NULL in case of error
305 */
306struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000307{
Harald Welte966636f2009-06-26 19:39:35 +0200308 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000309 struct ipaccess_head *hh;
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100310 int len, ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000311
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200312 if (!msg) {
313 *error = -ENOMEM;
314 return NULL;
315 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000316
317 /* first read our 3-byte header */
318 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100319 ret = recv(bfd->fd, msg->data, sizeof(*hh), 0);
320 if (ret == 0) {
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200321 msgb_free(msg);
322 *error = ret;
323 return NULL;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100324 } else if (ret != sizeof(*hh)) {
325 if (errno != EAGAIN)
326 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200327 msgb_free(msg);
328 *error = ret;
329 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000330 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200331
Harald Welte5fd8a542009-02-13 02:43:36 +0000332 msgb_put(msg, ret);
333
334 /* then read te length as specified in header */
335 msg->l2h = msg->data + sizeof(*hh);
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100336 len = ntohs(hh->len);
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100337
338 if (len < 0 || TS1_ALLOC_SIZE < len + sizeof(*hh)) {
339 LOGP(DINP, LOGL_ERROR, "Can not read this packet. %d avail\n", len);
340 msgb_free(msg);
341 *error = -EIO;
342 return NULL;
343 }
344
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100345 ret = recv(bfd->fd, msg->l2h, len, 0);
346 if (ret < len) {
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100347 LOGP(DINP, LOGL_ERROR, "short read! Got %d from %d\n", ret, len);
Harald Welte5c1e4582009-02-15 11:57:29 +0000348 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200349 *error = -EIO;
350 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000351 }
352 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200353
354 return msg;
355}
356
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200357int ipaccess_drop_oml(struct gsm_bts *bts)
358{
359 struct gsm_bts_trx *trx;
360 struct e1inp_ts *ts;
361 struct e1inp_line *line;
362 struct bsc_fd *bfd;
363
364 if (!bts || !bts->oml_link)
365 return -1;
366
367 /* send OML down */
368 ts = bts->oml_link->ts;
369 line = ts->line;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200370 e1inp_event(ts, EVT_E1_TEI_DN, bts->oml_link->tei, bts->oml_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200371
372 bfd = &ts->driver.ipaccess.fd;
373 bsc_unregister_fd(bfd);
374 close(bfd->fd);
375 bfd->fd = -1;
376
377 /* clean up OML and RSL */
378 e1inp_sign_link_destroy(bts->oml_link);
379 bts->oml_link = NULL;
380 bts->ip_access.flags = 0;
381
382 /* drop all RSL connections too */
383 llist_for_each_entry(trx, &bts->trx_list, list)
384 ipaccess_drop_rsl(trx);
385
386 /* kill the E1 line now... as we have no one left to use it */
387 talloc_free(line);
388
389 return -1;
390}
391
392static int ipaccess_drop(struct e1inp_ts *ts, struct bsc_fd *bfd)
393{
394 struct e1inp_sign_link *link;
395 int bts_nr;
396
397 if (!ts) {
398 /*
399 * If we don't have a TS this means that this is a RSL
400 * connection but we are not past the authentication
401 * handling yet. So we can safely delete this bfd and
402 * wait for a reconnect.
403 */
404 bsc_unregister_fd(bfd);
405 close(bfd->fd);
406 bfd->fd = -1;
407 talloc_free(bfd);
408 return -1;
409 }
410
411 /* attempt to find a signalling link */
412 if (ts->type == E1INP_TS_TYPE_SIGN) {
413 llist_for_each_entry(link, &ts->sign.sign_links, list) {
414 bts_nr = link->trx->bts->bts_nr;
415 /* we have issues just reconnecting RLS so we drop OML */
416 ipaccess_drop_oml(link->trx->bts);
417 return bts_nr;
418 }
419 }
420
421 /* error case */
422 LOGP(DINP, LOGL_ERROR, "Failed to find a signalling link for ts: %p\n", ts);
423 bsc_unregister_fd(bfd);
424 close(bfd->fd);
425 bfd->fd = -1;
426 return -1;
427}
428
429int ipaccess_drop_rsl(struct gsm_bts_trx *trx)
430{
431 struct bsc_fd *bfd;
432 struct e1inp_ts *ts;
433
434 if (!trx || !trx->rsl_link)
435 return -1;
436
437 /* send RSL down */
438 ts = trx->rsl_link->ts;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200439 e1inp_event(ts, EVT_E1_TEI_DN, trx->rsl_link->tei, trx->rsl_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200440
441 /* close the socket */
442 bfd = &ts->driver.ipaccess.fd;
443 bsc_unregister_fd(bfd);
444 close(bfd->fd);
445 bfd->fd = -1;
446
447 /* destroy */
448 e1inp_sign_link_destroy(trx->rsl_link);
449 trx->rsl_link = NULL;
450
451 return -1;
452}
453
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200454static int handle_ts1_read(struct bsc_fd *bfd)
455{
456 struct e1inp_line *line = bfd->data;
457 unsigned int ts_nr = bfd->priv_nr;
458 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
459 struct e1inp_sign_link *link;
460 struct msgb *msg;
461 struct ipaccess_head *hh;
Holger Hans Peter Freyther67b59612009-10-27 10:08:38 +0100462 int ret = 0, error;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200463
464 msg = ipaccess_read_msg(bfd, &error);
465 if (!msg) {
466 if (error == 0) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200467 int ret = ipaccess_drop(e1i_ts, bfd);
468 if (ret >= 0)
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100469 LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n",
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200470 ret);
471 else
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100472 LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n");
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200473 }
474 return error;
475 }
476
Sylvain Munaut86538c72009-09-30 22:35:04 +0200477 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000478
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200479 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000480 if (hh->proto == IPAC_PROTO_IPACCESS) {
481 ret = ipaccess_rcvmsg(line, msg, bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200482 if (ret < 0)
483 ipaccess_drop(e1i_ts, bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000484 msgb_free(msg);
485 return ret;
486 }
487 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
488 * might have free'd it !!! */
489
Harald Welte8175e952009-10-20 00:22:00 +0200490 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
Harald Welte5fd8a542009-02-13 02:43:36 +0000491 if (!link) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100492 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
493 "hh->proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000494 msgb_free(msg);
495 return -EIO;
496 }
497 msg->trx = link->trx;
498
Harald Welte8175e952009-10-20 00:22:00 +0200499 switch (link->type) {
500 case E1INP_SIGN_RSL:
Harald Welte1394fea2009-12-21 23:01:33 +0100501 if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) {
Harald Weltee73501a2009-10-21 21:16:00 +0200502 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte1394fea2009-12-21 23:01:33 +0100503 msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr);
Harald Weltee73501a2009-10-21 21:16:00 +0200504 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000505 ret = abis_rsl_rcvmsg(msg);
506 break;
Harald Welte8175e952009-10-20 00:22:00 +0200507 case E1INP_SIGN_OML:
Harald Welte88a412a2009-12-16 17:32:37 +0100508 if (!(msg->trx->bts->ip_access.flags & OML_UP)) {
Harald Welte8175e952009-10-20 00:22:00 +0200509 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte88a412a2009-12-16 17:32:37 +0100510 msg->trx->bts->ip_access.flags |= OML_UP;
Harald Welte7782c142009-02-15 03:39:51 +0000511 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000512 ret = abis_nm_rcvmsg(msg);
513 break;
514 default:
Harald Welteafdca0f2009-12-23 23:01:04 +0100515 LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000516 msgb_free(msg);
517 break;
518 }
519 return ret;
520}
521
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200522void ipaccess_prepend_header(struct msgb *msg, int proto)
523{
524 struct ipaccess_head *hh;
525
526 /* prepend the ip.access header */
527 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100528 hh->len = htons(msg->len - sizeof(*hh));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200529 hh->proto = proto;
530}
531
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200532static int ts_want_write(struct e1inp_ts *e1i_ts)
533{
534 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
535
536 return 0;
537}
538
539static void timeout_ts1_write(void *data)
540{
541 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
542
543 /* trigger write of ts1, due to tx delay timer */
544 ts_want_write(e1i_ts);
545}
546
Harald Welte5fd8a542009-02-13 02:43:36 +0000547static int handle_ts1_write(struct bsc_fd *bfd)
548{
549 struct e1inp_line *line = bfd->data;
550 unsigned int ts_nr = bfd->priv_nr;
551 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
552 struct e1inp_sign_link *sign_link;
553 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200554 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000555 int ret;
556
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200557 bfd->when &= ~BSC_FD_WRITE;
558
Harald Welte5fd8a542009-02-13 02:43:36 +0000559 /* get the next msg for this timeslot */
560 msg = e1inp_tx_ts(e1i_ts, &sign_link);
561 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200562 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000563 return 0;
564 }
565
Harald Welte5fd8a542009-02-13 02:43:36 +0000566 switch (sign_link->type) {
567 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200568 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000569 break;
570 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200571 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000572 break;
573 default:
574 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200575 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000576 return -EINVAL;
577 }
578
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200579 msg->l2h = msg->data;
Harald Welte8175e952009-10-20 00:22:00 +0200580 ipaccess_prepend_header(msg, sign_link->tei);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200581
582 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000583
584 ret = send(bfd->fd, msg->data, msg->len, 0);
585 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200586
587 /* set tx delay timer for next event */
588 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
589 e1i_ts->sign.tx_timer.data = e1i_ts;
Holger Hans Peter Freyther63cb4472010-04-11 10:10:04 +0200590
591 /* Reducing this might break the nanoBTS 900 init. */
Holger Hans Peter Freytherd85642a2010-04-26 16:02:04 +0800592 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
Harald Welte5fd8a542009-02-13 02:43:36 +0000593
594 return ret;
595}
596
Harald Welte5fd8a542009-02-13 02:43:36 +0000597/* callback from select.c in case one of the fd's can be read/written */
598static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
599{
600 struct e1inp_line *line = bfd->data;
601 unsigned int ts_nr = bfd->priv_nr;
602 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000603 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000604 int rc = 0;
605
Harald Welteedb37782009-05-01 14:59:07 +0000606 /* In case of early RSL we might not yet have a line */
607
608 if (line)
609 e1i_ts = &line->ts[idx];
610
611 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000612 if (what & BSC_FD_READ)
613 rc = handle_ts1_read(bfd);
614 if (what & BSC_FD_WRITE)
615 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000616 } else
Harald Welteafdca0f2009-12-23 23:01:04 +0100617 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000618
619 return rc;
620}
621
Harald Welte5fd8a542009-02-13 02:43:36 +0000622struct e1inp_driver ipaccess_driver = {
623 .name = "ip.access",
624 .want_write = ts_want_write,
Holger Hans Peter Freytherd49fc5a2010-11-15 20:36:21 +0100625 .default_delay = 0,
Harald Welte5fd8a542009-02-13 02:43:36 +0000626};
627
Harald Welteedb37782009-05-01 14:59:07 +0000628/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000629static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
630{
Harald Welte5fd8a542009-02-13 02:43:36 +0000631 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000632 int idx = 0;
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100633 int i;
Harald Welteedb37782009-05-01 14:59:07 +0000634 struct e1inp_line *line;
635 struct e1inp_ts *e1i_ts;
636 struct bsc_fd *bfd;
637 struct sockaddr_in sa;
638 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000639
Harald Welteedb37782009-05-01 14:59:07 +0000640 if (!(what & BSC_FD_READ))
641 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000642
Harald Welteedb37782009-05-01 14:59:07 +0000643 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
644 if (ret < 0) {
645 perror("accept");
646 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000647 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100648 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
649 inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000650
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100651 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000652 if (!line) {
653 close(ret);
654 return -ENOMEM;
655 }
Harald Welteedb37782009-05-01 14:59:07 +0000656 line->driver = &ipaccess_driver;
657 //line->driver_data = e1h;
658 /* create virrtual E1 timeslots for signalling */
659 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000660
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100661 /* initialize the fds */
662 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
663 line->ts[i].driver.ipaccess.fd.fd = -1;
664
Harald Welteedb37782009-05-01 14:59:07 +0000665 e1i_ts = &line->ts[idx];
666
667 bfd = &e1i_ts->driver.ipaccess.fd;
668 bfd->fd = ret;
669 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100670 bfd->priv_nr = PRIV_OML;
Harald Welteedb37782009-05-01 14:59:07 +0000671 bfd->cb = ipaccess_fd_cb;
672 bfd->when = BSC_FD_READ;
673 ret = bsc_register_fd(bfd);
674 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100675 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000676 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200677 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000678 return ret;
679 }
680
681 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100682 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000683
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200684 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200685 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000686}
687
Harald Welte5c1e4582009-02-15 11:57:29 +0000688static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
689{
Harald Welteedb37782009-05-01 14:59:07 +0000690 struct sockaddr_in sa;
691 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200692 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000693 int ret;
694
Harald Welteedb37782009-05-01 14:59:07 +0000695 if (!(what & BSC_FD_READ))
696 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000697
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100698 bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200699 if (!bfd)
700 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200701
Harald Welteedb37782009-05-01 14:59:07 +0000702 /* Some BTS has connected to us, but we don't know yet which line
703 * (as created by the OML link) to associate it with. Thus, we
Holger Hans Peter Freytherc7df7c62009-11-15 13:50:39 +0100704 * allocate a temporary bfd until we have received ID from BTS */
Harald Welteedb37782009-05-01 14:59:07 +0000705
706 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
707 if (bfd->fd < 0) {
708 perror("accept");
709 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000710 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100711 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 +0100712 bfd->priv_nr = PRIV_RSL;
Harald Welteedb37782009-05-01 14:59:07 +0000713 bfd->cb = ipaccess_fd_cb;
714 bfd->when = BSC_FD_READ;
715 ret = bsc_register_fd(bfd);
716 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100717 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000718 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200719 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000720 return ret;
721 }
722 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
723 ret = write(bfd->fd, id_req, sizeof(id_req));
724
Harald Welte5c1e4582009-02-15 11:57:29 +0000725 return 0;
726}
727
Harald Welte25de9912009-04-30 15:53:07 +0000728/* Actively connect to a BTS. Currently used by ipaccess-config.c */
729int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
730{
731 struct e1inp_ts *e1i_ts = &line->ts[0];
732 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
733 int ret, on = 1;
734
735 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
736 bfd->cb = ipaccess_fd_cb;
737 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
738 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100739 bfd->priv_nr = PRIV_OML;
Harald Welte25de9912009-04-30 15:53:07 +0000740
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100741 if (bfd->fd < 0) {
742 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
743 return -EIO;
744 }
745
Harald Welte25de9912009-04-30 15:53:07 +0000746 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
747
748 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
749 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100750 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
Harald Welte25de9912009-04-30 15:53:07 +0000751 close(bfd->fd);
752 return ret;
753 }
754
755 ret = bsc_register_fd(bfd);
756 if (ret < 0) {
757 close(bfd->fd);
758 return ret;
759 }
760
761 line->driver = &ipaccess_driver;
762
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200763 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200764 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000765}
766
Harald Welteedb37782009-05-01 14:59:07 +0000767int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000768{
Harald Welte5c1e4582009-02-15 11:57:29 +0000769 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000770
771 /* register the driver with the core */
772 /* FIXME: do this in the plugin initializer function */
773 ret = e1inp_driver_register(&ipaccess_driver);
774 if (ret)
775 return ret;
776
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100777 e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200778 if (!e1h)
779 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200780
Harald Welteedb37782009-05-01 14:59:07 +0000781 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000782
Harald Welte5c1e4582009-02-15 11:57:29 +0000783 /* Listen for OML connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200784 ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, 0, IPA_TCP_PORT_OML,
Harald Welte9b455bf2010-03-14 15:45:01 +0800785 listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000786 if (ret < 0)
787 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000788
Harald Welte5c1e4582009-02-15 11:57:29 +0000789 /* Listen for RSL connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200790 ret = make_sock(&e1h->rsl_listen_fd, IPPROTO_TCP, 0,
Harald Welte9b455bf2010-03-14 15:45:01 +0800791 IPA_TCP_PORT_RSL, rsl_listen_fd_cb);
792 if (ret < 0)
793 return ret;
794
Harald Welteedb37782009-05-01 14:59:07 +0000795 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000796}