blob: faa5458300b60961fc31063f58cdb12869275281 [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
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200106 while (len >= 2) {
107 len -= 2;
Harald Welteedb37782009-05-01 14:59:07 +0000108 t_len = *cur++;
109 t_tag = *cur++;
110
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200111 if (t_len > len + 1) {
112 LOGP(DMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len);
113 return -1;
114 }
115
Harald Welteedb37782009-05-01 14:59:07 +0000116 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
117
118 dec->lv[t_tag].len = t_len;
119 dec->lv[t_tag].val = cur;
120
121 cur += t_len;
Holger Hans Peter Freytherd9e81d02010-10-14 20:42:45 +0200122 len -= t_len;
Harald Welteedb37782009-05-01 14:59:07 +0000123 }
124 return 0;
125}
126
127struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
128 u_int16_t site_id, u_int16_t bts_id)
129{
Harald Weltee441d9c2009-06-21 16:17:15 +0200130 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000131
Harald Weltee441d9c2009-06-21 16:17:15 +0200132 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000133
134 if (!is_ipaccess_bts(bts))
135 continue;
136
137 if (bts->ip_access.site_id == site_id &&
138 bts->ip_access.bts_id == bts_id)
139 return bts;
140 }
141
142 return NULL;
143}
144
145static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
146 u_int16_t *trx_id)
147{
148 unsigned long ul;
149 char *endptr;
150 const char *nptr;
151
152 nptr = str;
153 ul = strtoul(nptr, &endptr, 10);
154 if (endptr <= nptr)
155 return -EINVAL;
156 if (site_id)
157 *site_id = ul & 0xffff;
158
159 if (*endptr++ != '/')
160 return -EINVAL;
161
162 nptr = endptr;
163 ul = strtoul(nptr, &endptr, 10);
164 if (endptr <= nptr)
165 return -EINVAL;
166 if (bts_id)
167 *bts_id = ul & 0xffff;
168
169 if (*endptr++ != '/')
170 return -EINVAL;
171
172 nptr = endptr;
173 ul = strtoul(nptr, &endptr, 10);
174 if (endptr <= nptr)
175 return -EINVAL;
176 if (trx_id)
177 *trx_id = ul & 0xffff;
178
179 return 0;
180}
181
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100182/* send the id ack */
183int ipaccess_send_id_ack(int fd)
184{
185 return write(fd, id_ack, sizeof(id_ack));
186}
187
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100188int ipaccess_send_id_req(int fd)
189{
190 return write(fd, id_req, sizeof(id_req));
191}
192
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200193/* base handling of the ip.access protocol */
194int ipaccess_rcvmsg_base(struct msgb *msg,
195 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000196{
Harald Welte5fd8a542009-02-13 02:43:36 +0000197 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000198 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000199
Harald Welte5fd8a542009-02-13 02:43:36 +0000200 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000201 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000202 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000203 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000204 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000205 DEBUGP(DMI, "PONG!\n");
206 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200207 case IPAC_MSGT_ID_ACK:
208 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100209 ret = ipaccess_send_id_ack(bfd->fd);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200210 break;
211 }
212 return 0;
213}
214
215static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
216 struct bsc_fd *bfd)
217{
218 struct tlv_parsed tlvp;
219 u_int8_t msg_type = *(msg->l2h);
220 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
221 struct gsm_bts *bts;
222
223 /* handle base messages */
224 ipaccess_rcvmsg_base(msg, bfd);
225
226 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000227 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000228 DEBUGP(DMI, "ID_RESP ");
229 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100230 ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
Harald Welteedb37782009-05-01 14:59:07 +0000231 msgb_l2len(msg)-2);
232 DEBUGP(DMI, "\n");
233
234 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
235 break;
236
237 /* lookup BTS, create sign_link, ... */
238 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
239 &site_id, &bts_id, &trx_id);
240 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
241 if (!bts) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100242 LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for "
Harald Welteedb37782009-05-01 14:59:07 +0000243 " %u/%u/%u, disconnecting\n", site_id, bts_id,
244 trx_id);
245 return -EIO;
246 }
247 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100248 if (bfd->priv_nr == PRIV_OML) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200249 /* drop any old oml connection */
250 ipaccess_drop_oml(bts);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100251 bts->oml_link = e1inp_sign_link_create(&line->ts[PRIV_OML - 1],
Harald Welteedb37782009-05-01 14:59:07 +0000252 E1INP_SIGN_OML, bts->c0,
Harald Welte8175e952009-10-20 00:22:00 +0200253 bts->oml_tei, 0);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100254 } else if (bfd->priv_nr == PRIV_RSL) {
Harald Welteedb37782009-05-01 14:59:07 +0000255 struct e1inp_ts *e1i_ts;
256 struct bsc_fd *newbfd;
Harald Welte8175e952009-10-20 00:22:00 +0200257 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200258
259 /* drop any old rsl connection */
260 ipaccess_drop_rsl(trx);
261
262 if (!bts->oml_link) {
263 bsc_unregister_fd(bfd);
264 close(bfd->fd);
265 bfd->fd = -1;
266 talloc_free(bfd);
267 return 0;
268 }
269
Harald Welteedb37782009-05-01 14:59:07 +0000270 bfd->data = line = bts->oml_link->ts->line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100271 e1i_ts = &line->ts[PRIV_RSL + trx_id - 1];
Harald Welteedb37782009-05-01 14:59:07 +0000272 newbfd = &e1i_ts->driver.ipaccess.fd;
Harald Welte8175e952009-10-20 00:22:00 +0200273 e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000274
Harald Welte8175e952009-10-20 00:22:00 +0200275 trx->rsl_link = e1inp_sign_link_create(e1i_ts,
276 E1INP_SIGN_RSL, trx,
277 trx->rsl_tei, 0);
Holger Hans Peter Freyther354ef812010-03-24 04:02:55 +0100278
Harald Welteedb37782009-05-01 14:59:07 +0000279 /* get rid of our old temporary bfd */
280 memcpy(newbfd, bfd, sizeof(*newbfd));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100281 newbfd->priv_nr = PRIV_RSL + trx_id;
Harald Welteedb37782009-05-01 14:59:07 +0000282 bsc_unregister_fd(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200283 bfd->fd = -1;
Harald Weltea4ffea92009-06-22 01:36:25 +0200284 talloc_free(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200285 bsc_register_fd(newbfd);
Harald Welteedb37782009-05-01 14:59:07 +0000286 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000287 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000288 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000289 return 0;
290}
291
Harald Welte88a412a2009-12-16 17:32:37 +0100292#define OML_UP 0x0001
293#define RSL_UP 0x0002
Harald Welte7782c142009-02-15 03:39:51 +0000294
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200295/*
296 * read one ipa message from the socket
297 * return NULL in case of error
298 */
299struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000300{
Harald Welte966636f2009-06-26 19:39:35 +0200301 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000302 struct ipaccess_head *hh;
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100303 int len, ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000304
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200305 if (!msg) {
306 *error = -ENOMEM;
307 return NULL;
308 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000309
310 /* first read our 3-byte header */
311 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100312 ret = recv(bfd->fd, msg->data, sizeof(*hh), 0);
313 if (ret == 0) {
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200314 msgb_free(msg);
315 *error = ret;
316 return NULL;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100317 } else if (ret != sizeof(*hh)) {
318 if (errno != EAGAIN)
319 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200320 msgb_free(msg);
321 *error = ret;
322 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000323 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200324
Harald Welte5fd8a542009-02-13 02:43:36 +0000325 msgb_put(msg, ret);
326
327 /* then read te length as specified in header */
328 msg->l2h = msg->data + sizeof(*hh);
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100329 len = ntohs(hh->len);
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100330
331 if (len < 0 || TS1_ALLOC_SIZE < len + sizeof(*hh)) {
332 LOGP(DINP, LOGL_ERROR, "Can not read this packet. %d avail\n", len);
333 msgb_free(msg);
334 *error = -EIO;
335 return NULL;
336 }
337
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100338 ret = recv(bfd->fd, msg->l2h, len, 0);
339 if (ret < len) {
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100340 LOGP(DINP, LOGL_ERROR, "short read! Got %d from %d\n", ret, len);
Harald Welte5c1e4582009-02-15 11:57:29 +0000341 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200342 *error = -EIO;
343 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000344 }
345 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200346
347 return msg;
348}
349
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200350int ipaccess_drop_oml(struct gsm_bts *bts)
351{
352 struct gsm_bts_trx *trx;
353 struct e1inp_ts *ts;
354 struct e1inp_line *line;
355 struct bsc_fd *bfd;
356
357 if (!bts || !bts->oml_link)
358 return -1;
359
360 /* send OML down */
361 ts = bts->oml_link->ts;
362 line = ts->line;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200363 e1inp_event(ts, EVT_E1_TEI_DN, bts->oml_link->tei, bts->oml_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200364
365 bfd = &ts->driver.ipaccess.fd;
366 bsc_unregister_fd(bfd);
367 close(bfd->fd);
368 bfd->fd = -1;
369
370 /* clean up OML and RSL */
371 e1inp_sign_link_destroy(bts->oml_link);
372 bts->oml_link = NULL;
373 bts->ip_access.flags = 0;
374
375 /* drop all RSL connections too */
376 llist_for_each_entry(trx, &bts->trx_list, list)
377 ipaccess_drop_rsl(trx);
378
379 /* kill the E1 line now... as we have no one left to use it */
380 talloc_free(line);
381
382 return -1;
383}
384
385static int ipaccess_drop(struct e1inp_ts *ts, struct bsc_fd *bfd)
386{
387 struct e1inp_sign_link *link;
388 int bts_nr;
389
390 if (!ts) {
391 /*
392 * If we don't have a TS this means that this is a RSL
393 * connection but we are not past the authentication
394 * handling yet. So we can safely delete this bfd and
395 * wait for a reconnect.
396 */
397 bsc_unregister_fd(bfd);
398 close(bfd->fd);
399 bfd->fd = -1;
400 talloc_free(bfd);
401 return -1;
402 }
403
404 /* attempt to find a signalling link */
405 if (ts->type == E1INP_TS_TYPE_SIGN) {
406 llist_for_each_entry(link, &ts->sign.sign_links, list) {
407 bts_nr = link->trx->bts->bts_nr;
408 /* we have issues just reconnecting RLS so we drop OML */
409 ipaccess_drop_oml(link->trx->bts);
410 return bts_nr;
411 }
412 }
413
414 /* error case */
415 LOGP(DINP, LOGL_ERROR, "Failed to find a signalling link for ts: %p\n", ts);
416 bsc_unregister_fd(bfd);
417 close(bfd->fd);
418 bfd->fd = -1;
419 return -1;
420}
421
422int ipaccess_drop_rsl(struct gsm_bts_trx *trx)
423{
424 struct bsc_fd *bfd;
425 struct e1inp_ts *ts;
426
427 if (!trx || !trx->rsl_link)
428 return -1;
429
430 /* send RSL down */
431 ts = trx->rsl_link->ts;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200432 e1inp_event(ts, EVT_E1_TEI_DN, trx->rsl_link->tei, trx->rsl_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200433
434 /* close the socket */
435 bfd = &ts->driver.ipaccess.fd;
436 bsc_unregister_fd(bfd);
437 close(bfd->fd);
438 bfd->fd = -1;
439
440 /* destroy */
441 e1inp_sign_link_destroy(trx->rsl_link);
442 trx->rsl_link = NULL;
443
444 return -1;
445}
446
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200447static int handle_ts1_read(struct bsc_fd *bfd)
448{
449 struct e1inp_line *line = bfd->data;
450 unsigned int ts_nr = bfd->priv_nr;
451 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
452 struct e1inp_sign_link *link;
453 struct msgb *msg;
454 struct ipaccess_head *hh;
Holger Hans Peter Freyther67b59612009-10-27 10:08:38 +0100455 int ret = 0, error;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200456
457 msg = ipaccess_read_msg(bfd, &error);
458 if (!msg) {
459 if (error == 0) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200460 int ret = ipaccess_drop(e1i_ts, bfd);
461 if (ret >= 0)
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100462 LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n",
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200463 ret);
464 else
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100465 LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n");
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200466 }
467 return error;
468 }
469
Sylvain Munaut86538c72009-09-30 22:35:04 +0200470 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000471
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200472 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000473 if (hh->proto == IPAC_PROTO_IPACCESS) {
474 ret = ipaccess_rcvmsg(line, msg, bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200475 if (ret < 0)
476 ipaccess_drop(e1i_ts, bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000477 msgb_free(msg);
478 return ret;
479 }
480 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
481 * might have free'd it !!! */
482
Harald Welte8175e952009-10-20 00:22:00 +0200483 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
Harald Welte5fd8a542009-02-13 02:43:36 +0000484 if (!link) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100485 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
486 "hh->proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000487 msgb_free(msg);
488 return -EIO;
489 }
490 msg->trx = link->trx;
491
Harald Welte8175e952009-10-20 00:22:00 +0200492 switch (link->type) {
493 case E1INP_SIGN_RSL:
Harald Welte1394fea2009-12-21 23:01:33 +0100494 if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) {
Harald Weltee73501a2009-10-21 21:16:00 +0200495 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte1394fea2009-12-21 23:01:33 +0100496 msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr);
Harald Weltee73501a2009-10-21 21:16:00 +0200497 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000498 ret = abis_rsl_rcvmsg(msg);
499 break;
Harald Welte8175e952009-10-20 00:22:00 +0200500 case E1INP_SIGN_OML:
Harald Welte88a412a2009-12-16 17:32:37 +0100501 if (!(msg->trx->bts->ip_access.flags & OML_UP)) {
Harald Welte8175e952009-10-20 00:22:00 +0200502 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte88a412a2009-12-16 17:32:37 +0100503 msg->trx->bts->ip_access.flags |= OML_UP;
Harald Welte7782c142009-02-15 03:39:51 +0000504 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000505 ret = abis_nm_rcvmsg(msg);
506 break;
507 default:
Harald Welteafdca0f2009-12-23 23:01:04 +0100508 LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000509 msgb_free(msg);
510 break;
511 }
512 return ret;
513}
514
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200515void ipaccess_prepend_header(struct msgb *msg, int proto)
516{
517 struct ipaccess_head *hh;
518
519 /* prepend the ip.access header */
520 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100521 hh->len = htons(msg->len - sizeof(*hh));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200522 hh->proto = proto;
523}
524
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200525static int ts_want_write(struct e1inp_ts *e1i_ts)
526{
527 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
528
529 return 0;
530}
531
532static void timeout_ts1_write(void *data)
533{
534 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
535
536 /* trigger write of ts1, due to tx delay timer */
537 ts_want_write(e1i_ts);
538}
539
Harald Welte5fd8a542009-02-13 02:43:36 +0000540static int handle_ts1_write(struct bsc_fd *bfd)
541{
542 struct e1inp_line *line = bfd->data;
543 unsigned int ts_nr = bfd->priv_nr;
544 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
545 struct e1inp_sign_link *sign_link;
546 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200547 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000548 int ret;
549
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200550 bfd->when &= ~BSC_FD_WRITE;
551
Harald Welte5fd8a542009-02-13 02:43:36 +0000552 /* get the next msg for this timeslot */
553 msg = e1inp_tx_ts(e1i_ts, &sign_link);
554 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200555 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000556 return 0;
557 }
558
Harald Welte5fd8a542009-02-13 02:43:36 +0000559 switch (sign_link->type) {
560 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200561 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000562 break;
563 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200564 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000565 break;
566 default:
567 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200568 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000569 return -EINVAL;
570 }
571
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200572 msg->l2h = msg->data;
Harald Welte8175e952009-10-20 00:22:00 +0200573 ipaccess_prepend_header(msg, sign_link->tei);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200574
575 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000576
577 ret = send(bfd->fd, msg->data, msg->len, 0);
578 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200579
580 /* set tx delay timer for next event */
581 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
582 e1i_ts->sign.tx_timer.data = e1i_ts;
Holger Hans Peter Freyther63cb4472010-04-11 10:10:04 +0200583
584 /* Reducing this might break the nanoBTS 900 init. */
585 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000586
587 return ret;
588}
589
Harald Welte5fd8a542009-02-13 02:43:36 +0000590/* callback from select.c in case one of the fd's can be read/written */
591static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
592{
593 struct e1inp_line *line = bfd->data;
594 unsigned int ts_nr = bfd->priv_nr;
595 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000596 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000597 int rc = 0;
598
Harald Welteedb37782009-05-01 14:59:07 +0000599 /* In case of early RSL we might not yet have a line */
600
601 if (line)
602 e1i_ts = &line->ts[idx];
603
604 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000605 if (what & BSC_FD_READ)
606 rc = handle_ts1_read(bfd);
607 if (what & BSC_FD_WRITE)
608 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000609 } else
Harald Welteafdca0f2009-12-23 23:01:04 +0100610 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000611
612 return rc;
613}
614
Harald Welte5fd8a542009-02-13 02:43:36 +0000615struct e1inp_driver ipaccess_driver = {
616 .name = "ip.access",
617 .want_write = ts_want_write,
618};
619
Harald Welteedb37782009-05-01 14:59:07 +0000620/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000621static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
622{
Harald Welte5fd8a542009-02-13 02:43:36 +0000623 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000624 int idx = 0;
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100625 int i;
Harald Welteedb37782009-05-01 14:59:07 +0000626 struct e1inp_line *line;
627 struct e1inp_ts *e1i_ts;
628 struct bsc_fd *bfd;
629 struct sockaddr_in sa;
630 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000631
Harald Welteedb37782009-05-01 14:59:07 +0000632 if (!(what & BSC_FD_READ))
633 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000634
Harald Welteedb37782009-05-01 14:59:07 +0000635 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
636 if (ret < 0) {
637 perror("accept");
638 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000639 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100640 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
641 inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000642
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100643 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000644 if (!line) {
645 close(ret);
646 return -ENOMEM;
647 }
Harald Welteedb37782009-05-01 14:59:07 +0000648 line->driver = &ipaccess_driver;
649 //line->driver_data = e1h;
650 /* create virrtual E1 timeslots for signalling */
651 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000652
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100653 /* initialize the fds */
654 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
655 line->ts[i].driver.ipaccess.fd.fd = -1;
656
Harald Welteedb37782009-05-01 14:59:07 +0000657 e1i_ts = &line->ts[idx];
658
659 bfd = &e1i_ts->driver.ipaccess.fd;
660 bfd->fd = ret;
661 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100662 bfd->priv_nr = PRIV_OML;
Harald Welteedb37782009-05-01 14:59:07 +0000663 bfd->cb = ipaccess_fd_cb;
664 bfd->when = BSC_FD_READ;
665 ret = bsc_register_fd(bfd);
666 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100667 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000668 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200669 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000670 return ret;
671 }
672
673 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100674 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000675
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200676 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200677 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000678}
679
Harald Welte5c1e4582009-02-15 11:57:29 +0000680static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
681{
Harald Welteedb37782009-05-01 14:59:07 +0000682 struct sockaddr_in sa;
683 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200684 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000685 int ret;
686
Harald Welteedb37782009-05-01 14:59:07 +0000687 if (!(what & BSC_FD_READ))
688 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000689
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100690 bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200691 if (!bfd)
692 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200693
Harald Welteedb37782009-05-01 14:59:07 +0000694 /* Some BTS has connected to us, but we don't know yet which line
695 * (as created by the OML link) to associate it with. Thus, we
Holger Hans Peter Freytherc7df7c62009-11-15 13:50:39 +0100696 * allocate a temporary bfd until we have received ID from BTS */
Harald Welteedb37782009-05-01 14:59:07 +0000697
698 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
699 if (bfd->fd < 0) {
700 perror("accept");
701 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000702 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100703 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 +0100704 bfd->priv_nr = PRIV_RSL;
Harald Welteedb37782009-05-01 14:59:07 +0000705 bfd->cb = ipaccess_fd_cb;
706 bfd->when = BSC_FD_READ;
707 ret = bsc_register_fd(bfd);
708 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100709 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000710 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200711 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000712 return ret;
713 }
714 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
715 ret = write(bfd->fd, id_req, sizeof(id_req));
716
Harald Welte5c1e4582009-02-15 11:57:29 +0000717 return 0;
718}
719
Harald Welte25de9912009-04-30 15:53:07 +0000720/* Actively connect to a BTS. Currently used by ipaccess-config.c */
721int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
722{
723 struct e1inp_ts *e1i_ts = &line->ts[0];
724 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
725 int ret, on = 1;
726
727 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
728 bfd->cb = ipaccess_fd_cb;
729 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
730 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100731 bfd->priv_nr = PRIV_OML;
Harald Welte25de9912009-04-30 15:53:07 +0000732
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100733 if (bfd->fd < 0) {
734 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
735 return -EIO;
736 }
737
Harald Welte25de9912009-04-30 15:53:07 +0000738 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
739
740 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
741 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100742 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
Harald Welte25de9912009-04-30 15:53:07 +0000743 close(bfd->fd);
744 return ret;
745 }
746
747 ret = bsc_register_fd(bfd);
748 if (ret < 0) {
749 close(bfd->fd);
750 return ret;
751 }
752
753 line->driver = &ipaccess_driver;
754
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200755 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200756 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000757}
758
Harald Welteedb37782009-05-01 14:59:07 +0000759int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000760{
Harald Welte5c1e4582009-02-15 11:57:29 +0000761 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000762
763 /* register the driver with the core */
764 /* FIXME: do this in the plugin initializer function */
765 ret = e1inp_driver_register(&ipaccess_driver);
766 if (ret)
767 return ret;
768
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100769 e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200770 if (!e1h)
771 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200772
Harald Welteedb37782009-05-01 14:59:07 +0000773 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000774
Harald Welte5c1e4582009-02-15 11:57:29 +0000775 /* Listen for OML connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200776 ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, 0, IPA_TCP_PORT_OML,
Harald Welte9b455bf2010-03-14 15:45:01 +0800777 listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000778 if (ret < 0)
779 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000780
Harald Welte5c1e4582009-02-15 11:57:29 +0000781 /* Listen for RSL connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200782 ret = make_sock(&e1h->rsl_listen_fd, IPPROTO_TCP, 0,
Harald Welte9b455bf2010-03-14 15:45:01 +0800783 IPA_TCP_PORT_RSL, rsl_listen_fd_cb);
784 if (ret < 0)
785 return ret;
786
Harald Welteedb37782009-05-01 14:59:07 +0000787 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000788}