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