blob: 4968e80937f15c83a5c779820fc14ce999cd11f7 [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
104 while (cur < buf + len) {
105 t_len = *cur++;
106 t_tag = *cur++;
107
108 DEBUGPC(DMI, "%s='%s' ", ipac_idtag_name(t_tag), cur);
109
110 dec->lv[t_tag].len = t_len;
111 dec->lv[t_tag].val = cur;
112
113 cur += t_len;
114 }
115 return 0;
116}
117
118struct gsm_bts *find_bts_by_unitid(struct gsm_network *net,
119 u_int16_t site_id, u_int16_t bts_id)
120{
Harald Weltee441d9c2009-06-21 16:17:15 +0200121 struct gsm_bts *bts;
Harald Welteedb37782009-05-01 14:59:07 +0000122
Harald Weltee441d9c2009-06-21 16:17:15 +0200123 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welteedb37782009-05-01 14:59:07 +0000124
125 if (!is_ipaccess_bts(bts))
126 continue;
127
128 if (bts->ip_access.site_id == site_id &&
129 bts->ip_access.bts_id == bts_id)
130 return bts;
131 }
132
133 return NULL;
134}
135
136static int parse_unitid(const char *str, u_int16_t *site_id, u_int16_t *bts_id,
137 u_int16_t *trx_id)
138{
139 unsigned long ul;
140 char *endptr;
141 const char *nptr;
142
143 nptr = str;
144 ul = strtoul(nptr, &endptr, 10);
145 if (endptr <= nptr)
146 return -EINVAL;
147 if (site_id)
148 *site_id = ul & 0xffff;
149
150 if (*endptr++ != '/')
151 return -EINVAL;
152
153 nptr = endptr;
154 ul = strtoul(nptr, &endptr, 10);
155 if (endptr <= nptr)
156 return -EINVAL;
157 if (bts_id)
158 *bts_id = ul & 0xffff;
159
160 if (*endptr++ != '/')
161 return -EINVAL;
162
163 nptr = endptr;
164 ul = strtoul(nptr, &endptr, 10);
165 if (endptr <= nptr)
166 return -EINVAL;
167 if (trx_id)
168 *trx_id = ul & 0xffff;
169
170 return 0;
171}
172
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100173/* send the id ack */
174int ipaccess_send_id_ack(int fd)
175{
176 return write(fd, id_ack, sizeof(id_ack));
177}
178
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100179int ipaccess_send_id_req(int fd)
180{
181 return write(fd, id_req, sizeof(id_req));
182}
183
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200184/* base handling of the ip.access protocol */
185int ipaccess_rcvmsg_base(struct msgb *msg,
186 struct bsc_fd *bfd)
Harald Welteedb37782009-05-01 14:59:07 +0000187{
Harald Welte5fd8a542009-02-13 02:43:36 +0000188 u_int8_t msg_type = *(msg->l2h);
Holger Freytherff9592f2009-03-09 16:17:14 +0000189 int ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000190
Harald Welte5fd8a542009-02-13 02:43:36 +0000191 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000192 case IPAC_MSGT_PING:
Harald Welteedb37782009-05-01 14:59:07 +0000193 ret = write(bfd->fd, pong, sizeof(pong));
Harald Welte5fd8a542009-02-13 02:43:36 +0000194 break;
Harald Welte4f361fc2009-02-15 15:32:53 +0000195 case IPAC_MSGT_PONG:
Harald Welte5fd8a542009-02-13 02:43:36 +0000196 DEBUGP(DMI, "PONG!\n");
197 break;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200198 case IPAC_MSGT_ID_ACK:
199 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Holger Hans Peter Freyther301e6282010-01-13 09:06:46 +0100200 ret = ipaccess_send_id_ack(bfd->fd);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200201 break;
202 }
203 return 0;
204}
205
206static int ipaccess_rcvmsg(struct e1inp_line *line, struct msgb *msg,
207 struct bsc_fd *bfd)
208{
209 struct tlv_parsed tlvp;
210 u_int8_t msg_type = *(msg->l2h);
211 u_int16_t site_id = 0, bts_id = 0, trx_id = 0;
212 struct gsm_bts *bts;
213
214 /* handle base messages */
215 ipaccess_rcvmsg_base(msg, bfd);
216
217 switch (msg_type) {
Harald Welte4f361fc2009-02-15 15:32:53 +0000218 case IPAC_MSGT_ID_RESP:
Harald Welteedb37782009-05-01 14:59:07 +0000219 DEBUGP(DMI, "ID_RESP ");
220 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherd3d5be12010-02-09 14:37:23 +0100221 ipaccess_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
Harald Welteedb37782009-05-01 14:59:07 +0000222 msgb_l2len(msg)-2);
223 DEBUGP(DMI, "\n");
224
225 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT))
226 break;
227
228 /* lookup BTS, create sign_link, ... */
229 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
230 &site_id, &bts_id, &trx_id);
231 bts = find_bts_by_unitid(e1h->gsmnet, site_id, bts_id);
232 if (!bts) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100233 LOGP(DINP, LOGL_ERROR, "Unable to find BTS configuration for "
Harald Welteedb37782009-05-01 14:59:07 +0000234 " %u/%u/%u, disconnecting\n", site_id, bts_id,
235 trx_id);
236 return -EIO;
237 }
238 DEBUGP(DINP, "Identified BTS %u/%u/%u\n", site_id, bts_id, trx_id);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100239 if (bfd->priv_nr == PRIV_OML) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200240 /* drop any old oml connection */
241 ipaccess_drop_oml(bts);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100242 bts->oml_link = e1inp_sign_link_create(&line->ts[PRIV_OML - 1],
Harald Welteedb37782009-05-01 14:59:07 +0000243 E1INP_SIGN_OML, bts->c0,
Harald Welte8175e952009-10-20 00:22:00 +0200244 bts->oml_tei, 0);
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100245 } else if (bfd->priv_nr == PRIV_RSL) {
Harald Welteedb37782009-05-01 14:59:07 +0000246 struct e1inp_ts *e1i_ts;
247 struct bsc_fd *newbfd;
Harald Welte8175e952009-10-20 00:22:00 +0200248 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, trx_id);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200249
250 /* drop any old rsl connection */
251 ipaccess_drop_rsl(trx);
252
253 if (!bts->oml_link) {
254 bsc_unregister_fd(bfd);
255 close(bfd->fd);
256 bfd->fd = -1;
257 talloc_free(bfd);
258 return 0;
259 }
260
Harald Welteedb37782009-05-01 14:59:07 +0000261 bfd->data = line = bts->oml_link->ts->line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100262 e1i_ts = &line->ts[PRIV_RSL + trx_id - 1];
Harald Welteedb37782009-05-01 14:59:07 +0000263 newbfd = &e1i_ts->driver.ipaccess.fd;
Harald Welte8175e952009-10-20 00:22:00 +0200264 e1inp_ts_config(e1i_ts, line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000265
Harald Welte8175e952009-10-20 00:22:00 +0200266 trx->rsl_link = e1inp_sign_link_create(e1i_ts,
267 E1INP_SIGN_RSL, trx,
268 trx->rsl_tei, 0);
Holger Hans Peter Freyther354ef812010-03-24 04:02:55 +0100269
Harald Welteedb37782009-05-01 14:59:07 +0000270 /* get rid of our old temporary bfd */
271 memcpy(newbfd, bfd, sizeof(*newbfd));
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100272 newbfd->priv_nr = PRIV_RSL + trx_id;
Harald Welteedb37782009-05-01 14:59:07 +0000273 bsc_unregister_fd(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200274 bfd->fd = -1;
Harald Weltea4ffea92009-06-22 01:36:25 +0200275 talloc_free(bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200276 bsc_register_fd(newbfd);
Harald Welteedb37782009-05-01 14:59:07 +0000277 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000278 break;
Harald Welte5fd8a542009-02-13 02:43:36 +0000279 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000280 return 0;
281}
282
Harald Welte88a412a2009-12-16 17:32:37 +0100283#define OML_UP 0x0001
284#define RSL_UP 0x0002
Harald Welte7782c142009-02-15 03:39:51 +0000285
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200286/*
287 * read one ipa message from the socket
288 * return NULL in case of error
289 */
290struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte5fd8a542009-02-13 02:43:36 +0000291{
Harald Welte966636f2009-06-26 19:39:35 +0200292 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "Abis/IP");
Harald Welte5fd8a542009-02-13 02:43:36 +0000293 struct ipaccess_head *hh;
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100294 int len, ret = 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000295
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200296 if (!msg) {
297 *error = -ENOMEM;
298 return NULL;
299 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000300
301 /* first read our 3-byte header */
302 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100303 ret = recv(bfd->fd, msg->data, sizeof(*hh), 0);
304 if (ret == 0) {
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200305 msgb_free(msg);
306 *error = ret;
307 return NULL;
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100308 } else if (ret != sizeof(*hh)) {
309 if (errno != EAGAIN)
310 LOGP(DINP, LOGL_ERROR, "recv error %d %s\n", ret, strerror(errno));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200311 msgb_free(msg);
312 *error = ret;
313 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000314 }
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200315
Harald Welte5fd8a542009-02-13 02:43:36 +0000316 msgb_put(msg, ret);
317
318 /* then read te length as specified in header */
319 msg->l2h = msg->data + sizeof(*hh);
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100320 len = ntohs(hh->len);
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100321
322 if (len < 0 || TS1_ALLOC_SIZE < len + sizeof(*hh)) {
323 LOGP(DINP, LOGL_ERROR, "Can not read this packet. %d avail\n", len);
324 msgb_free(msg);
325 *error = -EIO;
326 return NULL;
327 }
328
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100329 ret = recv(bfd->fd, msg->l2h, len, 0);
330 if (ret < len) {
Holger Hans Peter Freytherb3121c52010-03-24 08:40:55 +0100331 LOGP(DINP, LOGL_ERROR, "short read! Got %d from %d\n", ret, len);
Harald Welte5c1e4582009-02-15 11:57:29 +0000332 msgb_free(msg);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200333 *error = -EIO;
334 return NULL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000335 }
336 msgb_put(msg, ret);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200337
338 return msg;
339}
340
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200341int ipaccess_drop_oml(struct gsm_bts *bts)
342{
343 struct gsm_bts_trx *trx;
344 struct e1inp_ts *ts;
345 struct e1inp_line *line;
346 struct bsc_fd *bfd;
347
348 if (!bts || !bts->oml_link)
349 return -1;
350
351 /* send OML down */
352 ts = bts->oml_link->ts;
353 line = ts->line;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200354 e1inp_event(ts, EVT_E1_TEI_DN, bts->oml_link->tei, bts->oml_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200355
356 bfd = &ts->driver.ipaccess.fd;
357 bsc_unregister_fd(bfd);
358 close(bfd->fd);
359 bfd->fd = -1;
360
361 /* clean up OML and RSL */
362 e1inp_sign_link_destroy(bts->oml_link);
363 bts->oml_link = NULL;
364 bts->ip_access.flags = 0;
365
366 /* drop all RSL connections too */
367 llist_for_each_entry(trx, &bts->trx_list, list)
368 ipaccess_drop_rsl(trx);
369
370 /* kill the E1 line now... as we have no one left to use it */
371 talloc_free(line);
372
373 return -1;
374}
375
376static int ipaccess_drop(struct e1inp_ts *ts, struct bsc_fd *bfd)
377{
378 struct e1inp_sign_link *link;
379 int bts_nr;
380
381 if (!ts) {
382 /*
383 * If we don't have a TS this means that this is a RSL
384 * connection but we are not past the authentication
385 * handling yet. So we can safely delete this bfd and
386 * wait for a reconnect.
387 */
388 bsc_unregister_fd(bfd);
389 close(bfd->fd);
390 bfd->fd = -1;
391 talloc_free(bfd);
392 return -1;
393 }
394
395 /* attempt to find a signalling link */
396 if (ts->type == E1INP_TS_TYPE_SIGN) {
397 llist_for_each_entry(link, &ts->sign.sign_links, list) {
398 bts_nr = link->trx->bts->bts_nr;
399 /* we have issues just reconnecting RLS so we drop OML */
400 ipaccess_drop_oml(link->trx->bts);
401 return bts_nr;
402 }
403 }
404
405 /* error case */
406 LOGP(DINP, LOGL_ERROR, "Failed to find a signalling link for ts: %p\n", ts);
407 bsc_unregister_fd(bfd);
408 close(bfd->fd);
409 bfd->fd = -1;
410 return -1;
411}
412
413int ipaccess_drop_rsl(struct gsm_bts_trx *trx)
414{
415 struct bsc_fd *bfd;
416 struct e1inp_ts *ts;
417
418 if (!trx || !trx->rsl_link)
419 return -1;
420
421 /* send RSL down */
422 ts = trx->rsl_link->ts;
Holger Hans Peter Freytherf8eff2e2010-04-11 18:54:58 +0200423 e1inp_event(ts, EVT_E1_TEI_DN, trx->rsl_link->tei, trx->rsl_link->sapi);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200424
425 /* close the socket */
426 bfd = &ts->driver.ipaccess.fd;
427 bsc_unregister_fd(bfd);
428 close(bfd->fd);
429 bfd->fd = -1;
430
431 /* destroy */
432 e1inp_sign_link_destroy(trx->rsl_link);
433 trx->rsl_link = NULL;
434
435 return -1;
436}
437
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200438static int handle_ts1_read(struct bsc_fd *bfd)
439{
440 struct e1inp_line *line = bfd->data;
441 unsigned int ts_nr = bfd->priv_nr;
442 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
443 struct e1inp_sign_link *link;
444 struct msgb *msg;
445 struct ipaccess_head *hh;
Holger Hans Peter Freyther67b59612009-10-27 10:08:38 +0100446 int ret = 0, error;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200447
448 msg = ipaccess_read_msg(bfd, &error);
449 if (!msg) {
450 if (error == 0) {
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200451 int ret = ipaccess_drop(e1i_ts, bfd);
452 if (ret >= 0)
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100453 LOGP(DINP, LOGL_NOTICE, "BTS %u disappeared, dead socket\n",
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200454 ret);
455 else
Harald Welte (local)7971d3d2009-12-28 17:52:11 +0100456 LOGP(DINP, LOGL_NOTICE, "unknown BTS disappeared, dead socket\n");
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200457 }
458 return error;
459 }
460
Sylvain Munaut86538c72009-09-30 22:35:04 +0200461 DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000462
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200463 hh = (struct ipaccess_head *) msg->data;
Harald Welteedb37782009-05-01 14:59:07 +0000464 if (hh->proto == IPAC_PROTO_IPACCESS) {
465 ret = ipaccess_rcvmsg(line, msg, bfd);
Holger Hans Peter Freyther70402a42010-04-15 11:17:24 +0200466 if (ret < 0)
467 ipaccess_drop(e1i_ts, bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000468 msgb_free(msg);
469 return ret;
470 }
471 /* BIG FAT WARNING: bfd might no longer exist here, since ipaccess_rcvmsg()
472 * might have free'd it !!! */
473
Harald Welte8175e952009-10-20 00:22:00 +0200474 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
Harald Welte5fd8a542009-02-13 02:43:36 +0000475 if (!link) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100476 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
477 "hh->proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000478 msgb_free(msg);
479 return -EIO;
480 }
481 msg->trx = link->trx;
482
Harald Welte8175e952009-10-20 00:22:00 +0200483 switch (link->type) {
484 case E1INP_SIGN_RSL:
Harald Welte1394fea2009-12-21 23:01:33 +0100485 if (!(msg->trx->bts->ip_access.flags & (RSL_UP << msg->trx->nr))) {
Harald Weltee73501a2009-10-21 21:16:00 +0200486 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte1394fea2009-12-21 23:01:33 +0100487 msg->trx->bts->ip_access.flags |= (RSL_UP << msg->trx->nr);
Harald Weltee73501a2009-10-21 21:16:00 +0200488 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000489 ret = abis_rsl_rcvmsg(msg);
490 break;
Harald Welte8175e952009-10-20 00:22:00 +0200491 case E1INP_SIGN_OML:
Harald Welte88a412a2009-12-16 17:32:37 +0100492 if (!(msg->trx->bts->ip_access.flags & OML_UP)) {
Harald Welte8175e952009-10-20 00:22:00 +0200493 e1inp_event(e1i_ts, EVT_E1_TEI_UP, link->tei, link->sapi);
Harald Welte88a412a2009-12-16 17:32:37 +0100494 msg->trx->bts->ip_access.flags |= OML_UP;
Harald Welte7782c142009-02-15 03:39:51 +0000495 }
Harald Welte5fd8a542009-02-13 02:43:36 +0000496 ret = abis_nm_rcvmsg(msg);
497 break;
498 default:
Harald Welteafdca0f2009-12-23 23:01:04 +0100499 LOGP(DINP, LOGL_NOTICE, "Unknown IP.access protocol proto=0x%02x\n", hh->proto);
Harald Welte5fd8a542009-02-13 02:43:36 +0000500 msgb_free(msg);
501 break;
502 }
503 return ret;
504}
505
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200506void ipaccess_prepend_header(struct msgb *msg, int proto)
507{
508 struct ipaccess_head *hh;
509
510 /* prepend the ip.access header */
511 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Sylvain Munautd7d1c992009-10-29 16:33:59 +0100512 hh->len = htons(msg->len - sizeof(*hh));
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200513 hh->proto = proto;
514}
515
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200516static int ts_want_write(struct e1inp_ts *e1i_ts)
517{
518 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
519
520 return 0;
521}
522
523static void timeout_ts1_write(void *data)
524{
525 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
526
527 /* trigger write of ts1, due to tx delay timer */
528 ts_want_write(e1i_ts);
529}
530
Harald Welte5fd8a542009-02-13 02:43:36 +0000531static int handle_ts1_write(struct bsc_fd *bfd)
532{
533 struct e1inp_line *line = bfd->data;
534 unsigned int ts_nr = bfd->priv_nr;
535 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
536 struct e1inp_sign_link *sign_link;
537 struct msgb *msg;
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200538 u_int8_t proto;
Harald Welte5fd8a542009-02-13 02:43:36 +0000539 int ret;
540
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200541 bfd->when &= ~BSC_FD_WRITE;
542
Harald Welte5fd8a542009-02-13 02:43:36 +0000543 /* get the next msg for this timeslot */
544 msg = e1inp_tx_ts(e1i_ts, &sign_link);
545 if (!msg) {
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200546 /* no message after tx delay timer */
Harald Welte5fd8a542009-02-13 02:43:36 +0000547 return 0;
548 }
549
Harald Welte5fd8a542009-02-13 02:43:36 +0000550 switch (sign_link->type) {
551 case E1INP_SIGN_OML:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200552 proto = IPAC_PROTO_OML;
Harald Welte5fd8a542009-02-13 02:43:36 +0000553 break;
554 case E1INP_SIGN_RSL:
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200555 proto = IPAC_PROTO_RSL;
Harald Welte5fd8a542009-02-13 02:43:36 +0000556 break;
557 default:
558 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200559 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
Harald Welte5fd8a542009-02-13 02:43:36 +0000560 return -EINVAL;
561 }
562
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200563 msg->l2h = msg->data;
Harald Welte8175e952009-10-20 00:22:00 +0200564 ipaccess_prepend_header(msg, sign_link->tei);
Holger Hans Peter Freytherd5f05222009-08-18 10:05:45 +0200565
566 DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(msg->l2h, msgb_l2len(msg)));
Harald Welte5fd8a542009-02-13 02:43:36 +0000567
568 ret = send(bfd->fd, msg->data, msg->len, 0);
569 msgb_free(msg);
Andreas Eversberg38ae5cb2009-10-08 12:53:16 +0200570
571 /* set tx delay timer for next event */
572 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
573 e1i_ts->sign.tx_timer.data = e1i_ts;
Holger Hans Peter Freyther63cb4472010-04-11 10:10:04 +0200574
575 /* Reducing this might break the nanoBTS 900 init. */
576 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 100000);
Harald Welte5fd8a542009-02-13 02:43:36 +0000577
578 return ret;
579}
580
Harald Welte5fd8a542009-02-13 02:43:36 +0000581/* callback from select.c in case one of the fd's can be read/written */
582static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
583{
584 struct e1inp_line *line = bfd->data;
585 unsigned int ts_nr = bfd->priv_nr;
586 unsigned int idx = ts_nr-1;
Harald Welteedb37782009-05-01 14:59:07 +0000587 struct e1inp_ts *e1i_ts;
Harald Welte5fd8a542009-02-13 02:43:36 +0000588 int rc = 0;
589
Harald Welteedb37782009-05-01 14:59:07 +0000590 /* In case of early RSL we might not yet have a line */
591
592 if (line)
593 e1i_ts = &line->ts[idx];
594
595 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
Harald Welte5fd8a542009-02-13 02:43:36 +0000596 if (what & BSC_FD_READ)
597 rc = handle_ts1_read(bfd);
598 if (what & BSC_FD_WRITE)
599 rc = handle_ts1_write(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000600 } else
Harald Welteafdca0f2009-12-23 23:01:04 +0100601 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Harald Welte5fd8a542009-02-13 02:43:36 +0000602
603 return rc;
604}
605
Harald Welte5fd8a542009-02-13 02:43:36 +0000606struct e1inp_driver ipaccess_driver = {
607 .name = "ip.access",
608 .want_write = ts_want_write,
609};
610
Harald Welteedb37782009-05-01 14:59:07 +0000611/* callback of the OML listening filedescriptor */
Harald Welte5fd8a542009-02-13 02:43:36 +0000612static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
613{
Harald Welte5fd8a542009-02-13 02:43:36 +0000614 int ret;
Harald Welteedb37782009-05-01 14:59:07 +0000615 int idx = 0;
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100616 int i;
Harald Welteedb37782009-05-01 14:59:07 +0000617 struct e1inp_line *line;
618 struct e1inp_ts *e1i_ts;
619 struct bsc_fd *bfd;
620 struct sockaddr_in sa;
621 socklen_t sa_len = sizeof(sa);
Harald Welte5fd8a542009-02-13 02:43:36 +0000622
Harald Welteedb37782009-05-01 14:59:07 +0000623 if (!(what & BSC_FD_READ))
624 return 0;
Harald Welte5fd8a542009-02-13 02:43:36 +0000625
Harald Welteedb37782009-05-01 14:59:07 +0000626 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
627 if (ret < 0) {
628 perror("accept");
629 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000630 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100631 LOGP(DINP, LOGL_NOTICE, "accept()ed new OML link from %s\n",
632 inet_ntoa(sa.sin_addr));
Harald Welteedb37782009-05-01 14:59:07 +0000633
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100634 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
Harald Welteedb37782009-05-01 14:59:07 +0000635 if (!line) {
636 close(ret);
637 return -ENOMEM;
638 }
Harald Welteedb37782009-05-01 14:59:07 +0000639 line->driver = &ipaccess_driver;
640 //line->driver_data = e1h;
641 /* create virrtual E1 timeslots for signalling */
642 e1inp_ts_config(&line->ts[1-1], line, E1INP_TS_TYPE_SIGN);
Harald Welteedb37782009-05-01 14:59:07 +0000643
Holger Hans Peter Freytheredee7942010-03-24 11:20:27 +0100644 /* initialize the fds */
645 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
646 line->ts[i].driver.ipaccess.fd.fd = -1;
647
Harald Welteedb37782009-05-01 14:59:07 +0000648 e1i_ts = &line->ts[idx];
649
650 bfd = &e1i_ts->driver.ipaccess.fd;
651 bfd->fd = ret;
652 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100653 bfd->priv_nr = PRIV_OML;
Harald Welteedb37782009-05-01 14:59:07 +0000654 bfd->cb = ipaccess_fd_cb;
655 bfd->when = BSC_FD_READ;
656 ret = bsc_register_fd(bfd);
657 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100658 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000659 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200660 talloc_free(line);
Harald Welteedb37782009-05-01 14:59:07 +0000661 return ret;
662 }
663
664 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Holger Hans Peter Freyther3bc856b2010-02-07 12:04:07 +0100665 ret = ipaccess_send_id_req(bfd->fd);
Harald Welteedb37782009-05-01 14:59:07 +0000666
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200667 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200668 //return e1inp_line_register(line);
Harald Welte5fd8a542009-02-13 02:43:36 +0000669}
670
Harald Welte5c1e4582009-02-15 11:57:29 +0000671static int rsl_listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
672{
Harald Welteedb37782009-05-01 14:59:07 +0000673 struct sockaddr_in sa;
674 socklen_t sa_len = sizeof(sa);
Harald Weltea4ffea92009-06-22 01:36:25 +0200675 struct bsc_fd *bfd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000676 int ret;
677
Harald Welteedb37782009-05-01 14:59:07 +0000678 if (!(what & BSC_FD_READ))
679 return 0;
Harald Welte5c1e4582009-02-15 11:57:29 +0000680
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100681 bfd = talloc_zero(tall_bsc_ctx, struct bsc_fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200682 if (!bfd)
683 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200684
Harald Welteedb37782009-05-01 14:59:07 +0000685 /* Some BTS has connected to us, but we don't know yet which line
686 * (as created by the OML link) to associate it with. Thus, we
Holger Hans Peter Freytherc7df7c62009-11-15 13:50:39 +0100687 * allocate a temporary bfd until we have received ID from BTS */
Harald Welteedb37782009-05-01 14:59:07 +0000688
689 bfd->fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
690 if (bfd->fd < 0) {
691 perror("accept");
692 return bfd->fd;
Harald Welte5c1e4582009-02-15 11:57:29 +0000693 }
Harald Welteafdca0f2009-12-23 23:01:04 +0100694 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 +0100695 bfd->priv_nr = PRIV_RSL;
Harald Welteedb37782009-05-01 14:59:07 +0000696 bfd->cb = ipaccess_fd_cb;
697 bfd->when = BSC_FD_READ;
698 ret = bsc_register_fd(bfd);
699 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100700 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
Harald Welteedb37782009-05-01 14:59:07 +0000701 close(bfd->fd);
Harald Weltea4ffea92009-06-22 01:36:25 +0200702 talloc_free(bfd);
Harald Welteedb37782009-05-01 14:59:07 +0000703 return ret;
704 }
705 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
706 ret = write(bfd->fd, id_req, sizeof(id_req));
707
Harald Welte5c1e4582009-02-15 11:57:29 +0000708 return 0;
709}
710
Harald Welte25de9912009-04-30 15:53:07 +0000711/* Actively connect to a BTS. Currently used by ipaccess-config.c */
712int ipaccess_connect(struct e1inp_line *line, struct sockaddr_in *sa)
713{
714 struct e1inp_ts *e1i_ts = &line->ts[0];
715 struct bsc_fd *bfd = &e1i_ts->driver.ipaccess.fd;
716 int ret, on = 1;
717
718 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
719 bfd->cb = ipaccess_fd_cb;
720 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
721 bfd->data = line;
Holger Hans Peter Freytherdc6af632010-03-24 04:56:13 +0100722 bfd->priv_nr = PRIV_OML;
Harald Welte25de9912009-04-30 15:53:07 +0000723
Holger Hans Peter Freyther4d2d95b2010-02-19 13:07:05 +0100724 if (bfd->fd < 0) {
725 LOGP(DINP, LOGL_ERROR, "could not create TCP socket.\n");
726 return -EIO;
727 }
728
Harald Welte25de9912009-04-30 15:53:07 +0000729 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
730
731 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
732 if (ret < 0) {
Harald Welteafdca0f2009-12-23 23:01:04 +0100733 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
Harald Welte25de9912009-04-30 15:53:07 +0000734 close(bfd->fd);
735 return ret;
736 }
737
738 ret = bsc_register_fd(bfd);
739 if (ret < 0) {
740 close(bfd->fd);
741 return ret;
742 }
743
744 line->driver = &ipaccess_driver;
745
Holger Hans Peter Freyther09e364b2009-08-10 07:59:27 +0200746 return ret;
Harald Welte42581822009-08-08 16:12:58 +0200747 //return e1inp_line_register(line);
Harald Welte25de9912009-04-30 15:53:07 +0000748}
749
Harald Welteedb37782009-05-01 14:59:07 +0000750int ipaccess_setup(struct gsm_network *gsmnet)
Harald Welte5c1e4582009-02-15 11:57:29 +0000751{
Harald Welte5c1e4582009-02-15 11:57:29 +0000752 int ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000753
754 /* register the driver with the core */
755 /* FIXME: do this in the plugin initializer function */
756 ret = e1inp_driver_register(&ipaccess_driver);
757 if (ret)
758 return ret;
759
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100760 e1h = talloc_zero(tall_bsc_ctx, struct ia_e1_handle);
Harald Weltea4ffea92009-06-22 01:36:25 +0200761 if (!e1h)
762 return -ENOMEM;
Harald Weltea4ffea92009-06-22 01:36:25 +0200763
Harald Welteedb37782009-05-01 14:59:07 +0000764 e1h->gsmnet = gsmnet;
Harald Welte5fd8a542009-02-13 02:43:36 +0000765
Harald Welte5c1e4582009-02-15 11:57:29 +0000766 /* Listen for OML connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200767 ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, 0, IPA_TCP_PORT_OML,
Harald Welte9b455bf2010-03-14 15:45:01 +0800768 listen_fd_cb);
Harald Weltecf559782009-05-01 15:43:49 +0000769 if (ret < 0)
770 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000771
Harald Welte5c1e4582009-02-15 11:57:29 +0000772 /* Listen for RSL connections */
Harald Welte5540c4c2010-05-19 14:38:50 +0200773 ret = make_sock(&e1h->rsl_listen_fd, IPPROTO_TCP, 0,
Harald Welte9b455bf2010-03-14 15:45:01 +0800774 IPA_TCP_PORT_RSL, rsl_listen_fd_cb);
775 if (ret < 0)
776 return ret;
777
Harald Welteedb37782009-05-01 14:59:07 +0000778 return ret;
Harald Welte5fd8a542009-02-13 02:43:36 +0000779}