blob: 6b15e4792a089e201641bab9c38f58ddb8dacfc6 [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
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010036#include <osmocom/core/select.h>
37#include <osmocom/gsm/tlv.h>
38#include <osmocom/core/msgb.h>
39#include <osmocom/core/talloc.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 Weltef338a032011-01-14 15:55:42 +010048#include <openbsc/signal.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
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +020065/*
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 u_int8_t ipa_pong_msg[] = {
72 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
73};
74
75const u_int8_t ipa_id_ack_msg[] = {
76 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
77};
78
79const u_int8_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};
Harald Welte5fd8a542009-02-13 02:43:36 +000090
Harald Welteedb37782009-05-01 14:59:07 +000091static const char *idtag_names[] = {
92 [IPAC_IDTAG_SERNR] = "Serial_Number",
93 [IPAC_IDTAG_UNITNAME] = "Unit_Name",
94 [IPAC_IDTAG_LOCATION1] = "Location_1",
95 [IPAC_IDTAG_LOCATION2] = "Location_2",
96 [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version",
97 [IPAC_IDTAG_SWVERSION] = "Software_Version",
98 [IPAC_IDTAG_IPADDR] = "IP_Address",
99 [IPAC_IDTAG_MACADDR] = "MAC_Address",
100 [IPAC_IDTAG_UNIT] = "Unit_ID",
101};
102
Pablo Neira Ayuso66add642011-04-07 14:15:11 +0200103const char *ipaccess_idtag_name(int tag)
Harald Welte5fd8a542009-02-13 02:43:36 +0000104{
Harald Welteedb37782009-05-01 14:59:07 +0000105 if (tag >= ARRAY_SIZE(idtag_names))
106 return "unknown";
107
108 return idtag_names[tag];
109}
110
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100111int ipaccess_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welteedb37782009-05-01 14:59:07 +0000112{
113 u_int8_t t_len;
114 u_int8_t t_tag;
115 u_int8_t *cur = buf;
116
Holger Hans Peter Freyther949e0ba2010-10-14 17:21:04 +0200117 memset(dec, 0, sizeof(*dec));
118
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200119 while (len >= 2) {
120 len -= 2;
Harald Welteedb37782009-05-01 14:59:07 +0000121 t_len = *cur++;
122 t_tag = *cur++;
123
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200124 if (t_len > len + 1) {
125 LOGP(DMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
126 return -1;
127 }
128
Pablo Neira Ayuso66add642011-04-07 14:15:11 +0200129 DEBUGPC(DMI, "%s='%s' ", ipaccess_idtag_name(t_tag), cur);
Harald Welteedb37782009-05-01 14:59:07 +0000130
131 dec->lv[t_tag].len = t_len;
132 dec->lv[t_tag].val = cur;
133
134 cur += t_len;
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200135 len -= t_len;
Harald Welteedb37782009-05-01 14:59:07 +0000136 }
137 return 0;
138}
139
140struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
141 u_int16_t site_id, u_int16_t bts_id)
142{
Harald Weltee441d9c2009-06-21 16:17:15 +0200143 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000144
Harald Weltee441d9c2009-06-21 16:17:15 +0200145 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000146
147 if (!is_ipaccess_bts(bts))
148 continue;
149
150 if (bts->ip_access.site_id == site_id &&
151 bts->ip_access.bts_id == bts_id)
152 return bts;
153 }
154
155 return NULL;
156}
157
158static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
159 u_int16_t *trx_id)
160{
161 unsigned long ul;
162 char *endptr;
163 const char *nptr;
164
165 nptr = str;
166 ul = strtoul(nptr, &endptr, 10);
167 if (endptr <= nptr)
168 return -EINVAL;
169 if (site_id)
170 *site_id = ul & 0xffff;
171
172 if (*endptr++ != '/')
173 return -EINVAL;
174
175 nptr = endptr;
176 ul = strtoul(nptr, &endptr, 10);
177 if (endptr <= nptr)
178 return -EINVAL;
179 if (bts_id)
180 *bts_id = ul & 0xffff;
181
182 if (*endptr++ != '/')
183 return -EINVAL;
184
185 nptr = endptr;
186 ul = strtoul(nptr, &endptr, 10);
187 if (endptr <= nptr)
188 return -EINVAL;
189 if (trx_id)
190 *trx_id = ul & 0xffff;
191
192 return 0;
193}
194
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200195static int ipaccess_send(int fd, const void *msg, size_t msglen)
196{
197 int ret;
198
199 ret = write(fd, msg, msglen);
200 if (ret < 0)
201 return ret;
202 if (ret < msglen) {
203 DEBUGP(DINP, "ipaccess_send: short write\n");
204 return -EIO;
205 }
206 return ret;
207}
208
209int ipaccess_send_pong(int fd)
210{
211 return ipaccess_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
212}
213
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100214int ipaccess_send_id_ack(int fd)
215{
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200216 return ipaccess_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100217}
218
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100219int ipaccess_send_id_req(int fd)
220{
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200221 return ipaccess_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100222}
223
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200224/* base handling of the ip.access protocol */
225int ipaccess_rcvmsg_base(struct msgb *msg,
226 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000227{
Harald Welte5fd8a542009-02-13 02:43:36 +0000228 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000229 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000230
Harald Welte5fd8a542009-02-13 02:43:36 +0000231 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000232 case IPAC_MSGT_PING:
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200233 ret = ipaccess_send_pong(bfd->fd);
Harald Welte5fd8a542009-02-13 02:43:36 +0000234 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000235 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000236 DEBUGP(DMI, "PONG!\n");
237 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200238 case IPAC_MSGT_ID_ACK:
239 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100240 ret = ipaccess_send_id_ack(bfd->fd);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200241 break;
242 }
243 return 0;
244}
245
246static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
247 struct bsc_fd *bfd)
248{
249 struct tlv_parsed tlvp;
250 u_int8_t msg_type = *(msg->l2h);
251 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
252 struct gsm_bts *bts;
Holger Hans Peter Freythere3839802010-10-14 21:17:30 +0200253 char *unitid;
254 int len;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200255
256 /* handle base messages */
257 ipaccess_rcvmsg_base(msg, bfd);
258
259 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000260 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000261 DEBUGP(DMI, "ID_RESP ");
262 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100263 ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
Harald Welteedb37782009-05-01 14:59:07 +0000264 msgb_l2len(msg)-2);
265 DEBUGP(DMI, "\n");
266
267 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
268 break;
269
Holger Hans Peter Freythere3839802010-10-14 21:17:30 +0200270 len = TLVP_LEN(&tlvp, IPAC_IDTAG_UNIT);
271 if (len < 1)
272 break;
273
Harald Welteedb37782009-05-01 14:59:07 +0000274 /* lookup BTS, create sign_link, ... */
Holger Hans Peter Freythere3839802010-10-14 21:17:30 +0200275 unitid = (char *) TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT);
276 unitid[len - 1] = '\0';
277 parse_unitid(unitid, &site_id, &bts_id, &trx_id);
Harald Welteedb37782009-05-01 14:59:07 +0000278 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
279 if (!bts) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100280 LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for "
Harald Welteedb37782009-05-01 14:59:07 +0000281 " %u/%u/%u, disconnecting\n", site_id, bts_id,
282 trx_id);
283 return -EIO;
284 }
285 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100286 if (bfd->priv_nr == PRIV_OML) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200287 /* drop any old oml connection */
288 ipaccess_drop_oml(bts);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100289 bts->oml_link = e1inp_sign_link_create(&line->ts[PRIV_OML - 1],
Harald Welteedb37782009-05-01 14:59:07 +0000290 E1INP_SIGN_OML, bts->c0,
Harald Welte8175e952009-10-20 00:22:00 +0200291 bts->oml_tei, 0);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100292 } else if (bfd->priv_nr == PRIV_RSL) {
Harald Welteedb37782009-05-01 14:59:07 +0000293 struct e1inp_ts *e1i_ts;
294 struct bsc_fd *newbfd;
Harald Welte8175e952009-10-20 00:22:00 +0200295 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200296
297 /* drop any old rsl connection */
298 ipaccess_drop_rsl(trx);
299
300 if (!bts->oml_link) {
301 bsc_unregister_fd(bfd);
302 close(bfd->fd);
303 bfd->fd = -1;
304 talloc_free(bfd);
305 return 0;
306 }
307
Harald Welteedb37782009-05-01 14:59:07 +0000308 bfd->data = line = bts->oml_link->ts->line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100309 e1i_ts = &line->ts[PRIV_RSL + trx_id - 1];
Harald Welteedb37782009-05-01 14:59:07 +0000310 newbfd = &e1i_ts->driver.ipaccess.fd;
Harald Welte8175e952009-10-20 00:22:00 +0200311 e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000312
Harald Welte8175e952009-10-20 00:22:00 +0200313 trx->rsl_link = e1inp_sign_link_create(e1i_ts,
314 E1INP_SIGN_RSL, trx,
315 trx->rsl_tei, 0);
Holger Hans Peter Freytherd49fc5a2010-11-15 20:36:21 +0100316 trx->rsl_link->ts->sign.delay = 0;
Holger Hans Peter Freyther354ef812010-03-24 04:02:55 +0100317
Harald Welteedb37782009-05-01 14:59:07 +0000318 /* get rid of our old temporary bfd */
319 memcpy(newbfd, bfd, sizeof(*newbfd));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100320 newbfd->priv_nr = PRIV_RSL + trx_id;
Harald Welteedb37782009-05-01 14:59:07 +0000321 bsc_unregister_fd(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200322 bfd->fd = -1;
Harald Weltea4ffea92009-06-22 01:36:25 +0200323 talloc_free(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200324 bsc_register_fd(newbfd);
Harald Welteedb37782009-05-01 14:59:07 +0000325 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000326 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000327 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000328 return 0;
329}
330
Harald Welte88a412a2009-12-16 17:32:37 +0100331#define OML_UP 0x0001
332#define RSL_UP 0x0002
Harald Welte7782c142009-02-15 03:39:51 +0000333
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200334/*
335 * read one ipa message from the socket
336 * return NULL in case of error
337 */
338struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000339{
Harald Welte966636f2009-06-26 19:39:35 +0200340 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000341 struct ipaccess_head *hh;
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100342 int len, ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000343
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200344 if (!msg) {
345 *error = -ENOMEM;
346 return NULL;
347 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000348
349 /* first read our 3-byte header */
350 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100351 ret = recv(bfd->fd, msg->data, sizeof(*hh), 0);
352 if (ret == 0) {
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200353 msgb_free(msg);
354 *error = ret;
355 return NULL;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100356 } else if (ret != sizeof(*hh)) {
357 if (errno != EAGAIN)
358 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200359 msgb_free(msg);
360 *error = ret;
361 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000362 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200363
Harald Welte5fd8a542009-02-13 02:43:36 +0000364 msgb_put(msg, ret);
365
366 /* then read te length as specified in header */
367 msg->l2h = msg->data + sizeof(*hh);
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100368 len = ntohs(hh->len);
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100369
370 if (len < 0 || TS1_ALLOC_SIZE < len + sizeof(*hh)) {
371 LOGP(DINP, LOGL_ERROR, "Can not read this packet. %d avail\n", len);
372 msgb_free(msg);
373 *error = -EIO;
374 return NULL;
375 }
376
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100377 ret = recv(bfd->fd, msg->l2h, len, 0);
378 if (ret < len) {
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100379 LOGP(DINP, LOGL_ERROR, "short read! Got %d from %d\n", ret, len);
Harald Welte5c1e4582009-02-15 11:57:29 +0000380 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200381 *error = -EIO;
382 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000383 }
384 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200385
386 return msg;
387}
388
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200389int ipaccess_drop_oml(struct gsm_bts *bts)
390{
391 struct gsm_bts_trx *trx;
392 struct e1inp_ts *ts;
393 struct e1inp_line *line;
394 struct bsc_fd *bfd;
395
396 if (!bts || !bts->oml_link)
397 return -1;
398
399 /* send OML down */
400 ts = bts->oml_link->ts;
401 line = ts->line;
Harald Weltef338a032011-01-14 15:55:42 +0100402 e1inp_event(ts, S_INP_TEI_DN, bts->oml_link->tei, bts->oml_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200403
404 bfd = &ts->driver.ipaccess.fd;
405 bsc_unregister_fd(bfd);
406 close(bfd->fd);
407 bfd->fd = -1;
408
409 /* clean up OML and RSL */
410 e1inp_sign_link_destroy(bts->oml_link);
411 bts->oml_link = NULL;
412 bts->ip_access.flags = 0;
413
414 /* drop all RSL connections too */
415 llist_for_each_entry(trx, &bts->trx_list, list)
416 ipaccess_drop_rsl(trx);
417
418 /* kill the E1 line now... as we have no one left to use it */
419 talloc_free(line);
420
421 return -1;
422}
423
424static int ipaccess_drop(struct e1inp_ts *ts, struct bsc_fd *bfd)
425{
426 struct e1inp_sign_link *link;
427 int bts_nr;
428
429 if (!ts) {
430 /*
431 * If we don't have a TS this means that this is a RSL
432 * connection but we are not past the authentication
433 * handling yet. So we can safely delete this bfd and
434 * wait for a reconnect.
435 */
436 bsc_unregister_fd(bfd);
437 close(bfd->fd);
438 bfd->fd = -1;
439 talloc_free(bfd);
440 return -1;
441 }
442
443 /* attempt to find a signalling link */
444 if (ts->type == E1INP_TS_TYPE_SIGN) {
445 llist_for_each_entry(link, &ts->sign.sign_links, list) {
446 bts_nr = link->trx->bts->bts_nr;
447 /* we have issues just reconnecting RLS so we drop OML */
448 ipaccess_drop_oml(link->trx->bts);
449 return bts_nr;
450 }
451 }
452
453 /* error case */
454 LOGP(DINP, LOGL_ERROR, "Failed to find a signalling link for ts: %p\n", ts);
455 bsc_unregister_fd(bfd);
456 close(bfd->fd);
457 bfd->fd = -1;
458 return -1;
459}
460
461int ipaccess_drop_rsl(struct gsm_bts_trx *trx)
462{
463 struct bsc_fd *bfd;
464 struct e1inp_ts *ts;
465
466 if (!trx || !trx->rsl_link)
467 return -1;
468
469 /* send RSL down */
470 ts = trx->rsl_link->ts;
Harald Weltef338a032011-01-14 15:55:42 +0100471 e1inp_event(ts, S_INP_TEI_DN, trx->rsl_link->tei, trx->rsl_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200472
473 /* close the socket */
474 bfd = &ts->driver.ipaccess.fd;
475 bsc_unregister_fd(bfd);
476 close(bfd->fd);
477 bfd->fd = -1;
478
479 /* destroy */
480 e1inp_sign_link_destroy(trx->rsl_link);
481 trx->rsl_link = NULL;
482
483 return -1;
484}
485
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200486static int handle_ts1_read(struct bsc_fd *bfd)
487{
488 struct e1inp_line *line = bfd->data;
489 unsigned int ts_nr = bfd->priv_nr;
490 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
491 struct e1inp_sign_link *link;
492 struct msgb *msg;
493 struct ipaccess_head *hh;
Holger Hans Peter Freyther67b59612009-10-27 10:08:38 +0100494 int ret = 0, error;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200495
496 msg = ipaccess_read_msg(bfd, &error);
497 if (!msg) {
498 if (error == 0) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200499 int ret = ipaccess_drop(e1i_ts, bfd);
500 if (ret >= 0)
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100501 LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n",
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200502 ret);
503 else
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100504 LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n");
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200505 }
506 return error;
507 }
508
Sylvain Munaut86538c72009-09-30 22:35:04 +0200509 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000510
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200511 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000512 if (hh->proto == IPAC_PROTO_IPACCESS) {
513 ret = ipaccess_rcvmsg(line, msg, bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200514 if (ret < 0)
515 ipaccess_drop(e1i_ts, bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000516 msgb_free(msg);
517 return ret;
518 }
519 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
520 * might have free'd it !!! */
521
Harald Welte8175e952009-10-20 00:22:00 +0200522 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
Harald Welte5fd8a542009-02-13 02:43:36 +0000523 if (!link) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100524 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
525 "hh->proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000526 msgb_free(msg);
527 return -EIO;
528 }
529 msg->trx = link->trx;
530
Harald Welte8175e952009-10-20 00:22:00 +0200531 switch (link->type) {
532 case E1INP_SIGN_RSL:
Harald Welte1394fea2009-12-21 23:01:33 +0100533 if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) {
Harald Weltef338a032011-01-14 15:55:42 +0100534 e1inp_event(e1i_ts, S_INP_TEI_UP, link->tei, link->sapi);
Harald Welte1394fea2009-12-21 23:01:33 +0100535 msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr);
Harald Weltee73501a2009-10-21 21:16:00 +0200536 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000537 ret = abis_rsl_rcvmsg(msg);
538 break;
Harald Welte8175e952009-10-20 00:22:00 +0200539 case E1INP_SIGN_OML:
Harald Welte88a412a2009-12-16 17:32:37 +0100540 if (!(msg->trx->bts->ip_access.flags & OML_UP)) {
Harald Weltef338a032011-01-14 15:55:42 +0100541 e1inp_event(e1i_ts, S_INP_TEI_UP, link->tei, link->sapi);
Harald Welte88a412a2009-12-16 17:32:37 +0100542 msg->trx->bts->ip_access.flags |= OML_UP;
Harald Welte7782c142009-02-15 03:39:51 +0000543 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000544 ret = abis_nm_rcvmsg(msg);
545 break;
546 default:
Harald Welteafdca0f2009-12-23 23:01:04 +0100547 LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000548 msgb_free(msg);
549 break;
550 }
551 return ret;
552}
553
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200554void ipaccess_prepend_header(struct msgb *msg, int proto)
555{
556 struct ipaccess_head *hh;
557
558 /* prepend the ip.access header */
559 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100560 hh->len = htons(msg->len - sizeof(*hh));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200561 hh->proto = proto;
562}
563
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200564static int ts_want_write(struct e1inp_ts *e1i_ts)
565{
566 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
567
568 return 0;
569}
570
571static void timeout_ts1_write(void *data)
572{
573 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
574
575 /* trigger write of ts1, due to tx delay timer */
576 ts_want_write(e1i_ts);
577}
578
Harald Welte5fd8a542009-02-13 02:43:36 +0000579static int handle_ts1_write(struct bsc_fd *bfd)
580{
581 struct e1inp_line *line = bfd->data;
582 unsigned int ts_nr = bfd->priv_nr;
583 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
584 struct e1inp_sign_link *sign_link;
585 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200586 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000587 int ret;
588
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200589 bfd->when &= ~BSC_FD_WRITE;
590
Harald Welte5fd8a542009-02-13 02:43:36 +0000591 /* get the next msg for this timeslot */
592 msg = e1inp_tx_ts(e1i_ts, &sign_link);
593 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200594 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000595 return 0;
596 }
597
Harald Welte5fd8a542009-02-13 02:43:36 +0000598 switch (sign_link->type) {
599 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200600 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000601 break;
602 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200603 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000604 break;
605 default:
606 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200607 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000608 return -EINVAL;
609 }
610
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200611 msg->l2h = msg->data;
Harald Welte8175e952009-10-20 00:22:00 +0200612 ipaccess_prepend_header(msg, sign_link->tei);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200613
614 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000615
616 ret = send(bfd->fd, msg->data, msg->len, 0);
617 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200618
619 /* set tx delay timer for next event */
620 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
621 e1i_ts->sign.tx_timer.data = e1i_ts;
Holger Hans Peter Freyther63cb4472010-04-11 10:10:04 +0200622
623 /* Reducing this might break the nanoBTS 900 init. */
Holger Hans Peter Freytherd85642a2010-04-26 16:02:04 +0800624 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
Harald Welte5fd8a542009-02-13 02:43:36 +0000625
626 return ret;
627}
628
Harald Welte5fd8a542009-02-13 02:43:36 +0000629/* callback from select.c in case one of the fd's can be read/written */
630static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
631{
632 struct e1inp_line *line = bfd->data;
633 unsigned int ts_nr = bfd->priv_nr;
634 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000635 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000636 int rc = 0;
637
Harald Welteedb37782009-05-01 14:59:07 +0000638 /* In case of early RSL we might not yet have a line */
639
640 if (line)
641 e1i_ts = &line->ts[idx];
642
643 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000644 if (what & BSC_FD_READ)
645 rc = handle_ts1_read(bfd);
646 if (what & BSC_FD_WRITE)
647 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000648 } else
Harald Welteafdca0f2009-12-23 23:01:04 +0100649 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000650
651 return rc;
652}
653
Harald Welte5fd8a542009-02-13 02:43:36 +0000654struct e1inp_driver ipaccess_driver = {
655 .name = "ip.access",
656 .want_write = ts_want_write,
Holger Hans Peter Freytherd49fc5a2010-11-15 20:36:21 +0100657 .default_delay = 0,
Harald Welte5fd8a542009-02-13 02:43:36 +0000658};
659
Harald Welteedb37782009-05-01 14:59:07 +0000660/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000661static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
662{
Harald Welte5fd8a542009-02-13 02:43:36 +0000663 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000664 int idx = 0;
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100665 int i;
Harald Welteedb37782009-05-01 14:59:07 +0000666 struct e1inp_line *line;
667 struct e1inp_ts *e1i_ts;
668 struct bsc_fd *bfd;
669 struct sockaddr_in sa;
670 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000671
Harald Welteedb37782009-05-01 14:59:07 +0000672 if (!(what & BSC_FD_READ))
673 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000674
Harald Welteedb37782009-05-01 14:59:07 +0000675 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
676 if (ret < 0) {
677 perror("accept");
678 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000679 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100680 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
681 inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000682
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100683 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000684 if (!line) {
685 close(ret);
686 return -ENOMEM;
687 }
Harald Welteedb37782009-05-01 14:59:07 +0000688 line->driver = &ipaccess_driver;
689 //line->driver_data = e1h;
690 /* create virrtual E1 timeslots for signalling */
691 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000692
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100693 /* initialize the fds */
694 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
695 line->ts[i].driver.ipaccess.fd.fd = -1;
696
Harald Welteedb37782009-05-01 14:59:07 +0000697 e1i_ts = &line->ts[idx];
698
699 bfd = &e1i_ts->driver.ipaccess.fd;
700 bfd->fd = ret;
701 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100702 bfd->priv_nr = PRIV_OML;
Harald Welteedb37782009-05-01 14:59:07 +0000703 bfd->cb = ipaccess_fd_cb;
704 bfd->when = BSC_FD_READ;
705 ret = bsc_register_fd(bfd);
706 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100707 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000708 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200709 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000710 return ret;
711 }
712
713 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100714 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000715
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200716 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200717 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000718}
719
Harald Welte5c1e4582009-02-15 11:57:29 +0000720static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
721{
Harald Welteedb37782009-05-01 14:59:07 +0000722 struct sockaddr_in sa;
723 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200724 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000725 int ret;
726
Harald Welteedb37782009-05-01 14:59:07 +0000727 if (!(what & BSC_FD_READ))
728 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000729
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100730 bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200731 if (!bfd)
732 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200733
Harald Welteedb37782009-05-01 14:59:07 +0000734 /* Some BTS has connected to us, but we don't know yet which line
735 * (as created by the OML link) to associate it with. Thus, we
Holger Hans Peter Freytherc7df7c62009-11-15 13:50:39 +0100736 * allocate a temporary bfd until we have received ID from BTS */
Harald Welteedb37782009-05-01 14:59:07 +0000737
738 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
739 if (bfd->fd < 0) {
740 perror("accept");
741 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000742 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100743 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 +0100744 bfd->priv_nr = PRIV_RSL;
Harald Welteedb37782009-05-01 14:59:07 +0000745 bfd->cb = ipaccess_fd_cb;
746 bfd->when = BSC_FD_READ;
747 ret = bsc_register_fd(bfd);
748 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100749 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000750 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200751 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000752 return ret;
753 }
754 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200755 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000756
Harald Welte5c1e4582009-02-15 11:57:29 +0000757 return 0;
758}
759
Harald Welte25de9912009-04-30 15:53:07 +0000760/* Actively connect to a BTS. Currently used by ipaccess-config.c */
761int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
762{
763 struct e1inp_ts *e1i_ts = &line->ts[0];
764 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
765 int ret, on = 1;
766
767 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
768 bfd->cb = ipaccess_fd_cb;
769 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
770 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100771 bfd->priv_nr = PRIV_OML;
Harald Welte25de9912009-04-30 15:53:07 +0000772
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100773 if (bfd->fd < 0) {
774 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
775 return -EIO;
776 }
777
Harald Welte25de9912009-04-30 15:53:07 +0000778 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
779
780 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
781 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100782 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
Harald Welte25de9912009-04-30 15:53:07 +0000783 close(bfd->fd);
784 return ret;
785 }
786
787 ret = bsc_register_fd(bfd);
788 if (ret < 0) {
789 close(bfd->fd);
790 return ret;
791 }
792
793 line->driver = &ipaccess_driver;
794
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200795 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200796 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000797}
798
Harald Welteedb37782009-05-01 14:59:07 +0000799int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000800{
Harald Welte5c1e4582009-02-15 11:57:29 +0000801 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000802
803 /* register the driver with the core */
804 /* FIXME: do this in the plugin initializer function */
805 ret = e1inp_driver_register(&ipaccess_driver);
806 if (ret)
807 return ret;
808
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100809 e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200810 if (!e1h)
811 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200812
Harald Welteedb37782009-05-01 14:59:07 +0000813 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000814
Harald Welte5c1e4582009-02-15 11:57:29 +0000815 /* Listen for OML connections */
Pablo Neira Ayuso165fe562011-04-05 18:33:24 +0200816 ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, INADDR_ANY,
817 IPA_TCP_PORT_OML, 0, listen_fd_cb, NULL);
Harald Weltecf559782009-05-01 15:43:49 +0000818 if (ret < 0)
819 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000820
Harald Welte5c1e4582009-02-15 11:57:29 +0000821 /* Listen for RSL connections */
Pablo Neira Ayuso165fe562011-04-05 18:33:24 +0200822 ret = make_sock(&e1h->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY,
823 IPA_TCP_PORT_RSL, 0, rsl_listen_fd_cb, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800824 if (ret < 0)
825 return ret;
826
Harald Welteedb37782009-05-01 14:59:07 +0000827 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000828}