blob: 3854b0671b5ed3fefcb42ea2f570e8b3efe2863f [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis input driver for HSL Femto */
2
3/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2011 by On-Waves
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23/* HSL uses a much more primitive/simplified version of the IPA multiplex.
24 *
25 * They have taken out the nice parts like the ID_GET / ID_RESP for resolving
26 * the UNIT ID, as well as the keepalive ping/pong messages. Furthermore, the
27 * Stream Identifiers are fixed on the BTS side (RSL always 0, OML always 0xff)
28 * and both OML+RSL share a single TCP connection.
29 *
30 * Other oddities include the encapsulation of BSSGP messages in the L3_INFO IE
31 * of RSL
32 */
33
34#include "internal.h"
35
36#include <stdio.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include <errno.h>
40#include <string.h>
41#include <time.h>
42#include <sys/fcntl.h>
43#include <sys/socket.h>
44#include <sys/ioctl.h>
45#include <arpa/inet.h>
46
47#include <osmocom/core/select.h>
48#include <osmocom/gsm/tlv.h>
49#include <osmocom/core/msgb.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020050#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051#include <osmocom/core/logging.h>
Pablo Neira Ayusoe47a9782011-06-07 18:03:48 +020052#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020053#include <osmocom/core/socket.h>
Pablo Neira Ayuso0b099b22011-06-09 13:14:11 +020054#include <osmocom/abis/logging.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020055#include <talloc.h>
56
57#define HSL_TCP_PORT 2500
58#define HSL_PROTO_DEBUG 0xdd
59
60#define PRIV_OML 1
61#define PRIV_RSL 2
62
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020063static void *tall_hsl_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020064
65/* data structure for one E1 interface with A-bis */
66struct hsl_e1_handle {
67 struct osmo_fd listen_fd;
68 struct gsm_network *gsmnet;
69};
70
71static struct hsl_e1_handle *e1h;
72
73
74#define TS1_ALLOC_SIZE 900
75
76static int handle_ts1_read(struct osmo_fd *bfd)
77{
78 struct e1inp_line *line = bfd->data;
79 unsigned int ts_nr = bfd->priv_nr;
80 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020081 struct e1inp_sign_link *link;
82 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020083 struct msgb *msg;
84 int ret = 0, error;
85
86 msg = ipaccess_read_msg(bfd, &error);
87 if (!msg) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020088 if (e1i_ts->line->ops.error)
89 e1i_ts->line->ops.error(NULL, error);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020090 if (error == 0) {
91 osmo_fd_unregister(bfd);
92 close(bfd->fd);
93 bfd->fd = -1;
94 }
95 return error;
96 }
97 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
98
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020099 hh = (struct ipaccess_head *) msg->data;
100 if (hh->proto == HSL_PROTO_DEBUG) {
101 LOGP(DINP, LOGL_NOTICE, "HSL debug: %s\n", msg->data + sizeof(*hh));
102 msgb_free(msg);
103 return ret;
104 }
105
106 /* HSL proprietary RSL extension */
107 if (hh->proto == 0 && (msg->l2h[0] == 0x81 || msg->l2h[0] == 0x80)) {
108 if (!line->ops.sign_link_up) {
109 LOGP(DINP, LOGL_ERROR, "Fix your application, no "
110 "action set if the signalling link "
111 "becomes ready.\n");
112 return -EINVAL;
113 }
114 ret = line->ops.sign_link_up(msg, line);
115 if (ret < 0) {
116 /* FIXME: close connection */
117 if (line->ops.error)
118 line->ops.error(msg, -EBADMSG);
119 return ret;
120 } else if (ret == 1)
121 return 0;
122 /* else: continue... */
123 }
124
125#ifdef HSL_SR_1_0
126 /* HSL for whatever reason chose to use 0x81 instead of 0x80 for FOM */
127 if (hh->proto == 255 && msg->l2h[0] == (ABIS_OM_MDISC_FOM | 0x01))
128 msg->l2h[0] = ABIS_OM_MDISC_FOM;
129#endif
130 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
131 if (!link) {
132 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
133 "hh->proto=0x%02x\n", hh->proto);
134 msgb_free(msg);
135 return -EIO;
136 }
137 msg->dst = link;
138
139 /* XXX: better use e1inp_ts_rx? */
140 if (!e1i_ts->line->ops.sign_link) {
141 LOGP(DINP, LOGL_ERROR, "Fix your application, "
142 "no action set for signalling messages.\n");
143 return -ENOENT;
144 }
145 e1i_ts->line->ops.sign_link(msg, link);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200146
147 return ret;
148}
149
150static int ts_want_write(struct e1inp_ts *e1i_ts)
151{
152 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
153
154 return 0;
155}
156
157static void timeout_ts1_write(void *data)
158{
159 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
160
161 /* trigger write of ts1, due to tx delay timer */
162 ts_want_write(e1i_ts);
163}
164
165static int handle_ts1_write(struct osmo_fd *bfd)
166{
167 struct e1inp_line *line = bfd->data;
168 unsigned int ts_nr = bfd->priv_nr;
169 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
170 struct e1inp_sign_link *sign_link;
171 struct msgb *msg;
172 uint8_t proto;
173 int ret;
174
175 bfd->when &= ~BSC_FD_WRITE;
176
177 /* get the next msg for this timeslot */
178 msg = e1inp_tx_ts(e1i_ts, &sign_link);
179 if (!msg) {
180 /* no message after tx delay timer */
181 return 0;
182 }
183
184 switch (sign_link->type) {
185 case E1INP_SIGN_OML:
186 proto = IPAC_PROTO_OML;
187#ifdef HSL_SR_1_0
188 /* HSL uses 0x81 for FOM for some reason */
189 if (msg->data[0] == ABIS_OM_MDISC_FOM)
190 msg->data[0] = ABIS_OM_MDISC_FOM | 0x01;
191#endif
192 break;
193 case E1INP_SIGN_RSL:
194 proto = IPAC_PROTO_RSL;
195 break;
196 default:
197 msgb_free(msg);
198 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
199 return -EINVAL;
200 }
201
202 msg->l2h = msg->data;
203 ipaccess_prepend_header(msg, sign_link->tei);
204
205 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
206
207 ret = send(bfd->fd, msg->data, msg->len, 0);
208 msgb_free(msg);
209
210 /* set tx delay timer for next event */
211 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
212 e1i_ts->sign.tx_timer.data = e1i_ts;
213
214 /* Reducing this might break the nanoBTS 900 init. */
215 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
216
217 return ret;
218}
219
220/* callback from select.c in case one of the fd's can be read/written */
221static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what)
222{
223 struct e1inp_line *line = bfd->data;
224 unsigned int ts_nr = bfd->priv_nr;
225 unsigned int idx = ts_nr-1;
226 struct e1inp_ts *e1i_ts;
227 int rc = 0;
228
229 /* In case of early RSL we might not yet have a line */
230
231 if (line)
232 e1i_ts = &line->ts[idx];
233
234 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
235 if (what & BSC_FD_READ)
236 rc = handle_ts1_read(bfd);
237 if (what & BSC_FD_WRITE)
238 rc = handle_ts1_write(bfd);
239 } else
240 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
241
242 return rc;
243}
244
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200245static int hsl_line_update(struct e1inp_line *line,
246 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200247
248struct e1inp_driver hsl_driver = {
249 .name = "hsl",
250 .want_write = ts_want_write,
251 .line_update = hsl_line_update,
252 .default_delay = 0,
253};
254
255/* callback of the OML listening filedescriptor */
256static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
257{
258 int ret;
259 int idx = 0;
260 int i;
261 struct e1inp_line *line = listen_bfd->data;
262 struct e1inp_ts *e1i_ts;
263 struct osmo_fd *bfd;
264 struct sockaddr_in sa;
265 socklen_t sa_len = sizeof(sa);
266
267 if (!(what & BSC_FD_READ))
268 return 0;
269
270 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
271 if (ret < 0) {
272 perror("accept");
273 return ret;
274 }
275 LOGP(DINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n",
276 inet_ntoa(sa.sin_addr));
277
278 /* create virrtual E1 timeslots for signalling */
279 e1inp_ts_config_sign(&line->ts[1-1], line);
280
281 /* initialize the fds */
282 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
283 line->ts[i].driver.ipaccess.fd.fd = -1;
284
285 e1i_ts = &line->ts[idx];
286
287 bfd = &e1i_ts->driver.ipaccess.fd;
288 bfd->fd = ret;
289 bfd->data = line;
290 bfd->priv_nr = PRIV_OML;
291 bfd->cb = hsl_fd_cb;
292 bfd->when = BSC_FD_READ;
293 ret = osmo_fd_register(bfd);
294 if (ret < 0) {
295 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
296 close(bfd->fd);
297 talloc_free(line);
298 return ret;
299 }
300
301 return ret;
302}
303
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200304static int hsl_line_update(struct e1inp_line *line,
305 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200306{
307 int ret = -ENOENT;
308
309 switch(role) {
310 case E1INP_LINE_R_BSC:
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200311 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
312 "0.0.0.0", HSL_TCP_PORT, 1);
313 if (ret < 0)
314 return ret;
315
316 e1h->listen_fd.fd = ret;
317 e1h->listen_fd.when |= BSC_FD_READ;
318 e1h->listen_fd.cb = listen_fd_cb;
319 e1h->listen_fd.data = line;
320
321 if (osmo_fd_register(&e1h->listen_fd) < 0) {
322 close(ret);
323 return ret;
324 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200325 break;
326 case E1INP_LINE_R_BTS:
327 /* XXX: not implemented yet. */
328 break;
329 default:
330 break;
331 }
332 return ret;
333}
334
335int hsl_setup(struct gsm_network *gsmnet)
336{
337 e1h->gsmnet = gsmnet;
338 return 0;
339}
340
341void e1inp_hsl_init(void)
342{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200343 tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl");
344
345 e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346 if (!e1h)
347 return;
348
349 e1inp_driver_register(&hsl_driver);
350}