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