blob: 2ff9d3955e464f7c556db15f20f8dbe3987b2bc4 [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>
50#include <osmocom/gsm/abis/e1_input.h>
51#include <osmocom/core/logging.h>
52#include <osmocom/gsm/protocol/ipaccess.h>
53/*#include <openbsc/debug.h>
54#include <openbsc/gsm_data.h>
55#include <openbsc/abis_nm.h>
56#include <openbsc/abis_rsl.h>
57#include <openbsc/subchan_demux.h>
58#include <openbsc/e1_input.h>
59#include <openbsc/ipaccess.h>
60#include <openbsc/socket.h>
61#include <openbsc/signal.h> */
62#include <talloc.h>
63
64#define HSL_TCP_PORT 2500
65#define HSL_PROTO_DEBUG 0xdd
66
67#define PRIV_OML 1
68#define PRIV_RSL 2
69
70static void *tall_bsc_ctx;
71
72/* data structure for one E1 interface with A-bis */
73struct hsl_e1_handle {
74 struct osmo_fd listen_fd;
75 struct gsm_network *gsmnet;
76};
77
78static struct hsl_e1_handle *e1h;
79
80
81#define TS1_ALLOC_SIZE 900
82
83static int handle_ts1_read(struct osmo_fd *bfd)
84{
85 struct e1inp_line *line = bfd->data;
86 unsigned int ts_nr = bfd->priv_nr;
87 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
88 struct msgb *msg;
89 int ret = 0, error;
90
91 msg = ipaccess_read_msg(bfd, &error);
92 if (!msg) {
93 if (e1i_ts->line->rx_err)
94 e1i_ts->line->rx_err(error);
95 if (error == 0) {
96 osmo_fd_unregister(bfd);
97 close(bfd->fd);
98 bfd->fd = -1;
99 }
100 return error;
101 }
102 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
103
104 /* XXX better use e1inp_rx_ts. */
105 if (e1i_ts->line->rx)
106 e1i_ts->line->rx(msg, e1i_ts);
107
108 return ret;
109}
110
111static int ts_want_write(struct e1inp_ts *e1i_ts)
112{
113 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
114
115 return 0;
116}
117
118static void timeout_ts1_write(void *data)
119{
120 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
121
122 /* trigger write of ts1, due to tx delay timer */
123 ts_want_write(e1i_ts);
124}
125
126static int handle_ts1_write(struct osmo_fd *bfd)
127{
128 struct e1inp_line *line = bfd->data;
129 unsigned int ts_nr = bfd->priv_nr;
130 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
131 struct e1inp_sign_link *sign_link;
132 struct msgb *msg;
133 uint8_t proto;
134 int ret;
135
136 bfd->when &= ~BSC_FD_WRITE;
137
138 /* get the next msg for this timeslot */
139 msg = e1inp_tx_ts(e1i_ts, &sign_link);
140 if (!msg) {
141 /* no message after tx delay timer */
142 return 0;
143 }
144
145 switch (sign_link->type) {
146 case E1INP_SIGN_OML:
147 proto = IPAC_PROTO_OML;
148#ifdef HSL_SR_1_0
149 /* HSL uses 0x81 for FOM for some reason */
150 if (msg->data[0] == ABIS_OM_MDISC_FOM)
151 msg->data[0] = ABIS_OM_MDISC_FOM | 0x01;
152#endif
153 break;
154 case E1INP_SIGN_RSL:
155 proto = IPAC_PROTO_RSL;
156 break;
157 default:
158 msgb_free(msg);
159 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
160 return -EINVAL;
161 }
162
163 msg->l2h = msg->data;
164 ipaccess_prepend_header(msg, sign_link->tei);
165
166 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
167
168 ret = send(bfd->fd, msg->data, msg->len, 0);
169 msgb_free(msg);
170
171 /* set tx delay timer for next event */
172 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
173 e1i_ts->sign.tx_timer.data = e1i_ts;
174
175 /* Reducing this might break the nanoBTS 900 init. */
176 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
177
178 return ret;
179}
180
181/* callback from select.c in case one of the fd's can be read/written */
182static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what)
183{
184 struct e1inp_line *line = bfd->data;
185 unsigned int ts_nr = bfd->priv_nr;
186 unsigned int idx = ts_nr-1;
187 struct e1inp_ts *e1i_ts;
188 int rc = 0;
189
190 /* In case of early RSL we might not yet have a line */
191
192 if (line)
193 e1i_ts = &line->ts[idx];
194
195 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
196 if (what & BSC_FD_READ)
197 rc = handle_ts1_read(bfd);
198 if (what & BSC_FD_WRITE)
199 rc = handle_ts1_write(bfd);
200 } else
201 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
202
203 return rc;
204}
205
206static int hsl_line_update(struct e1inp_line *line, enum e1inp_line_role role);
207
208struct e1inp_driver hsl_driver = {
209 .name = "hsl",
210 .want_write = ts_want_write,
211 .line_update = hsl_line_update,
212 .default_delay = 0,
213};
214
215/* callback of the OML listening filedescriptor */
216static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
217{
218 int ret;
219 int idx = 0;
220 int i;
221 struct e1inp_line *line = listen_bfd->data;
222 struct e1inp_ts *e1i_ts;
223 struct osmo_fd *bfd;
224 struct sockaddr_in sa;
225 socklen_t sa_len = sizeof(sa);
226
227 if (!(what & BSC_FD_READ))
228 return 0;
229
230 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
231 if (ret < 0) {
232 perror("accept");
233 return ret;
234 }
235 LOGP(DINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n",
236 inet_ntoa(sa.sin_addr));
237
238 /* create virrtual E1 timeslots for signalling */
239 e1inp_ts_config_sign(&line->ts[1-1], line);
240
241 /* initialize the fds */
242 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
243 line->ts[i].driver.ipaccess.fd.fd = -1;
244
245 e1i_ts = &line->ts[idx];
246
247 bfd = &e1i_ts->driver.ipaccess.fd;
248 bfd->fd = ret;
249 bfd->data = line;
250 bfd->priv_nr = PRIV_OML;
251 bfd->cb = hsl_fd_cb;
252 bfd->when = BSC_FD_READ;
253 ret = osmo_fd_register(bfd);
254 if (ret < 0) {
255 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
256 close(bfd->fd);
257 talloc_free(line);
258 return ret;
259 }
260
261 return ret;
262}
263
264static int
265hsl_line_update(struct e1inp_line *line, enum e1inp_line_role role)
266{
267 int ret = -ENOENT;
268
269 switch(role) {
270 case E1INP_LINE_R_BSC:
271 ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, INADDR_ANY,
272 HSL_TCP_PORT, 0, listen_fd_cb, line);
273 break;
274 case E1INP_LINE_R_BTS:
275 /* XXX: not implemented yet. */
276 break;
277 default:
278 break;
279 }
280 return ret;
281}
282
283int hsl_setup(struct gsm_network *gsmnet)
284{
285 e1h->gsmnet = gsmnet;
286 return 0;
287}
288
289void e1inp_hsl_init(void)
290{
291 e1h = talloc_zero(tall_bsc_ctx, struct hsl_e1_handle);
292 if (!e1h)
293 return;
294
295 e1inp_driver_register(&hsl_driver);
296}