blob: 97475cf88810055f092a1e3f5f83b1087889c2bd [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>
52#include <osmocom/gsm/protocol/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020053#include <osmocom/core/socket.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020054#include <talloc.h>
55
56#define HSL_TCP_PORT 2500
57#define HSL_PROTO_DEBUG 0xdd
58
59#define PRIV_OML 1
60#define PRIV_RSL 2
61
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020062static void *tall_hsl_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020063
64/* data structure for one E1 interface with A-bis */
65struct hsl_e1_handle {
66 struct osmo_fd listen_fd;
67 struct gsm_network *gsmnet;
68};
69
70static struct hsl_e1_handle *e1h;
71
72
73#define TS1_ALLOC_SIZE 900
74
75static int handle_ts1_read(struct osmo_fd *bfd)
76{
77 struct e1inp_line *line = bfd->data;
78 unsigned int ts_nr = bfd->priv_nr;
79 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020080 struct e1inp_sign_link *link;
81 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020082 struct msgb *msg;
83 int ret = 0, error;
84
85 msg = ipaccess_read_msg(bfd, &error);
86 if (!msg) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020087 if (e1i_ts->line->ops.error)
88 e1i_ts->line->ops.error(NULL, error);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020089 if (error == 0) {
90 osmo_fd_unregister(bfd);
91 close(bfd->fd);
92 bfd->fd = -1;
93 }
94 return error;
95 }
96 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
97
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +020098 hh = (struct ipaccess_head *) msg->data;
99 if (hh->proto == HSL_PROTO_DEBUG) {
100 LOGP(DINP, LOGL_NOTICE, "HSL debug: %s\n", msg->data + sizeof(*hh));
101 msgb_free(msg);
102 return ret;
103 }
104
105 /* HSL proprietary RSL extension */
106 if (hh->proto == 0 && (msg->l2h[0] == 0x81 || msg->l2h[0] == 0x80)) {
107 if (!line->ops.sign_link_up) {
108 LOGP(DINP, LOGL_ERROR, "Fix your application, no "
109 "action set if the signalling link "
110 "becomes ready.\n");
111 return -EINVAL;
112 }
113 ret = line->ops.sign_link_up(msg, line);
114 if (ret < 0) {
115 /* FIXME: close connection */
116 if (line->ops.error)
117 line->ops.error(msg, -EBADMSG);
118 return ret;
119 } else if (ret == 1)
120 return 0;
121 /* else: continue... */
122 }
123
124#ifdef HSL_SR_1_0
125 /* HSL for whatever reason chose to use 0x81 instead of 0x80 for FOM */
126 if (hh->proto == 255 && msg->l2h[0] == (ABIS_OM_MDISC_FOM | 0x01))
127 msg->l2h[0] = ABIS_OM_MDISC_FOM;
128#endif
129 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
130 if (!link) {
131 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
132 "hh->proto=0x%02x\n", hh->proto);
133 msgb_free(msg);
134 return -EIO;
135 }
136 msg->dst = link;
137
138 /* XXX: better use e1inp_ts_rx? */
139 if (!e1i_ts->line->ops.sign_link) {
140 LOGP(DINP, LOGL_ERROR, "Fix your application, "
141 "no action set for signalling messages.\n");
142 return -ENOENT;
143 }
144 e1i_ts->line->ops.sign_link(msg, link);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200145
146 return ret;
147}
148
149static int ts_want_write(struct e1inp_ts *e1i_ts)
150{
151 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
152
153 return 0;
154}
155
156static void timeout_ts1_write(void *data)
157{
158 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
159
160 /* trigger write of ts1, due to tx delay timer */
161 ts_want_write(e1i_ts);
162}
163
164static int handle_ts1_write(struct osmo_fd *bfd)
165{
166 struct e1inp_line *line = bfd->data;
167 unsigned int ts_nr = bfd->priv_nr;
168 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
169 struct e1inp_sign_link *sign_link;
170 struct msgb *msg;
171 uint8_t proto;
172 int ret;
173
174 bfd->when &= ~BSC_FD_WRITE;
175
176 /* get the next msg for this timeslot */
177 msg = e1inp_tx_ts(e1i_ts, &sign_link);
178 if (!msg) {
179 /* no message after tx delay timer */
180 return 0;
181 }
182
183 switch (sign_link->type) {
184 case E1INP_SIGN_OML:
185 proto = IPAC_PROTO_OML;
186#ifdef HSL_SR_1_0
187 /* HSL uses 0x81 for FOM for some reason */
188 if (msg->data[0] == ABIS_OM_MDISC_FOM)
189 msg->data[0] = ABIS_OM_MDISC_FOM | 0x01;
190#endif
191 break;
192 case E1INP_SIGN_RSL:
193 proto = IPAC_PROTO_RSL;
194 break;
195 default:
196 msgb_free(msg);
197 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
198 return -EINVAL;
199 }
200
201 msg->l2h = msg->data;
202 ipaccess_prepend_header(msg, sign_link->tei);
203
204 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
205
206 ret = send(bfd->fd, msg->data, msg->len, 0);
207 msgb_free(msg);
208
209 /* set tx delay timer for next event */
210 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
211 e1i_ts->sign.tx_timer.data = e1i_ts;
212
213 /* Reducing this might break the nanoBTS 900 init. */
214 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
215
216 return ret;
217}
218
219/* callback from select.c in case one of the fd's can be read/written */
220static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what)
221{
222 struct e1inp_line *line = bfd->data;
223 unsigned int ts_nr = bfd->priv_nr;
224 unsigned int idx = ts_nr-1;
225 struct e1inp_ts *e1i_ts;
226 int rc = 0;
227
228 /* In case of early RSL we might not yet have a line */
229
230 if (line)
231 e1i_ts = &line->ts[idx];
232
233 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
234 if (what & BSC_FD_READ)
235 rc = handle_ts1_read(bfd);
236 if (what & BSC_FD_WRITE)
237 rc = handle_ts1_write(bfd);
238 } else
239 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
240
241 return rc;
242}
243
244static int hsl_line_update(struct e1inp_line *line, enum e1inp_line_role role);
245
246struct e1inp_driver hsl_driver = {
247 .name = "hsl",
248 .want_write = ts_want_write,
249 .line_update = hsl_line_update,
250 .default_delay = 0,
251};
252
253/* callback of the OML listening filedescriptor */
254static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
255{
256 int ret;
257 int idx = 0;
258 int i;
259 struct e1inp_line *line = listen_bfd->data;
260 struct e1inp_ts *e1i_ts;
261 struct osmo_fd *bfd;
262 struct sockaddr_in sa;
263 socklen_t sa_len = sizeof(sa);
264
265 if (!(what & BSC_FD_READ))
266 return 0;
267
268 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
269 if (ret < 0) {
270 perror("accept");
271 return ret;
272 }
273 LOGP(DINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n",
274 inet_ntoa(sa.sin_addr));
275
276 /* create virrtual E1 timeslots for signalling */
277 e1inp_ts_config_sign(&line->ts[1-1], line);
278
279 /* initialize the fds */
280 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
281 line->ts[i].driver.ipaccess.fd.fd = -1;
282
283 e1i_ts = &line->ts[idx];
284
285 bfd = &e1i_ts->driver.ipaccess.fd;
286 bfd->fd = ret;
287 bfd->data = line;
288 bfd->priv_nr = PRIV_OML;
289 bfd->cb = hsl_fd_cb;
290 bfd->when = BSC_FD_READ;
291 ret = osmo_fd_register(bfd);
292 if (ret < 0) {
293 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
294 close(bfd->fd);
295 talloc_free(line);
296 return ret;
297 }
298
299 return ret;
300}
301
302static int
303hsl_line_update(struct e1inp_line *line, enum e1inp_line_role role)
304{
305 int ret = -ENOENT;
306
307 switch(role) {
308 case E1INP_LINE_R_BSC:
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200309 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
310 "0.0.0.0", HSL_TCP_PORT, 1);
311 if (ret < 0)
312 return ret;
313
314 e1h->listen_fd.fd = ret;
315 e1h->listen_fd.when |= BSC_FD_READ;
316 e1h->listen_fd.cb = listen_fd_cb;
317 e1h->listen_fd.data = line;
318
319 if (osmo_fd_register(&e1h->listen_fd) < 0) {
320 close(ret);
321 return ret;
322 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200323 break;
324 case E1INP_LINE_R_BTS:
325 /* XXX: not implemented yet. */
326 break;
327 default:
328 break;
329 }
330 return ret;
331}
332
333int hsl_setup(struct gsm_network *gsmnet)
334{
335 e1h->gsmnet = gsmnet;
336 return 0;
337}
338
339void e1inp_hsl_init(void)
340{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200341 tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl");
342
343 e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200344 if (!e1h)
345 return;
346
347 e1inp_driver_register(&hsl_driver);
348}