blob: 36061f5cb0923ac7d147a288b19a21440bea2a83 [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 Ayuso9b3a33c2011-06-21 13:52:41 +020054#include <osmocom/abis/ipa.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
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020076static void hsl_drop(struct e1inp_line *line, struct osmo_fd *bfd)
77{
78 line->ops->sign_link_down(line);
79}
80
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +020081static int process_hsl_rsl(struct msgb *msg, struct e1inp_line *line,
82 struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020083{
84 char serno_buf[16];
85 uint8_t serno_len;
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +020086 struct e1inp_sign_link *sign_link;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020087 struct hsl_unit unit_data;
88
89 switch (msg->l2h[1]) {
90 case 0x80:
91 /*, contains Serial Number + SW version */
92 if (msg->l2h[2] != 0xc0)
93 break;
94 serno_len = msg->l2h[3];
95 if (serno_len > sizeof(serno_buf)-1)
96 serno_len = sizeof(serno_buf)-1;
97 memcpy(serno_buf, msg->l2h+4, serno_len);
98 serno_buf[serno_len] = '\0';
99 unit_data.serno = strtoul(serno_buf, NULL, 10);
100
101 if (!line->ops->sign_link_up) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200102 LOGP(DINP, LOGL_ERROR,
103 "Unable to set signal link, closing socket.\n");
104 osmo_fd_unregister(bfd);
105 close(bfd->fd);
106 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200107 return -EINVAL;
108 }
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200109 sign_link = line->ops->sign_link_up(&unit_data,
110 line, E1INP_SIGN_NONE);
111 if (sign_link == NULL) {
112 LOGP(DINP, LOGL_ERROR,
113 "Unable to set signal link, closing socket.\n");
114 osmo_fd_unregister(bfd);
115 close(bfd->fd);
116 bfd->fd = -1;
117 return -EINVAL;
118 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200119 msgb_free(msg);
120 return 1; /* == we have taken over the msg */
121 case 0x82:
122 /* FIXME: do something with BSSGP, i.e. forward it over
123 * NSIP to OsmoSGSN */
124 msgb_free(msg);
125 return 1;
126 }
127 return 0;
128}
129
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200130static int handle_ts1_read(struct osmo_fd *bfd)
131{
132 struct e1inp_line *line = bfd->data;
133 unsigned int ts_nr = bfd->priv_nr;
134 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200135 struct e1inp_sign_link *link;
136 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200137 struct msgb *msg;
138 int ret = 0, error;
139
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200140 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200141 if (error < 0)
142 return error;
143 else if (error == 0) {
144 hsl_drop(e1i_ts->line, bfd);
145 LOGP(DINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200146 return error;
147 }
148 DEBUGP(DMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
149
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200150 hh = (struct ipaccess_head *) msg->data;
151 if (hh->proto == HSL_PROTO_DEBUG) {
152 LOGP(DINP, LOGL_NOTICE, "HSL debug: %s\n", msg->data + sizeof(*hh));
153 msgb_free(msg);
154 return ret;
155 }
156
157 /* HSL proprietary RSL extension */
158 if (hh->proto == 0 && (msg->l2h[0] == 0x81 || msg->l2h[0] == 0x80)) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200159 ret = process_hsl_rsl(msg, line, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200160 if (ret < 0) {
161 hsl_drop(e1i_ts->line, bfd);
162 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200163 } else if (ret == 1)
164 return 0;
165 /* else: continue... */
166 }
167
168#ifdef HSL_SR_1_0
169 /* HSL for whatever reason chose to use 0x81 instead of 0x80 for FOM */
170 if (hh->proto == 255 && msg->l2h[0] == (ABIS_OM_MDISC_FOM | 0x01))
171 msg->l2h[0] = ABIS_OM_MDISC_FOM;
172#endif
173 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
174 if (!link) {
175 LOGP(DINP, LOGL_ERROR, "no matching signalling link for "
176 "hh->proto=0x%02x\n", hh->proto);
177 msgb_free(msg);
178 return -EIO;
179 }
180 msg->dst = link;
181
182 /* XXX: better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200183 if (!e1i_ts->line->ops->sign_link) {
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200184 LOGP(DINP, LOGL_ERROR, "Fix your application, "
185 "no action set for signalling messages.\n");
186 return -ENOENT;
187 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200188 e1i_ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200189
190 return ret;
191}
192
193static int ts_want_write(struct e1inp_ts *e1i_ts)
194{
195 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
196
197 return 0;
198}
199
200static void timeout_ts1_write(void *data)
201{
202 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
203
204 /* trigger write of ts1, due to tx delay timer */
205 ts_want_write(e1i_ts);
206}
207
208static int handle_ts1_write(struct osmo_fd *bfd)
209{
210 struct e1inp_line *line = bfd->data;
211 unsigned int ts_nr = bfd->priv_nr;
212 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
213 struct e1inp_sign_link *sign_link;
214 struct msgb *msg;
215 uint8_t proto;
216 int ret;
217
218 bfd->when &= ~BSC_FD_WRITE;
219
220 /* get the next msg for this timeslot */
221 msg = e1inp_tx_ts(e1i_ts, &sign_link);
222 if (!msg) {
223 /* no message after tx delay timer */
224 return 0;
225 }
226
227 switch (sign_link->type) {
228 case E1INP_SIGN_OML:
229 proto = IPAC_PROTO_OML;
230#ifdef HSL_SR_1_0
231 /* HSL uses 0x81 for FOM for some reason */
232 if (msg->data[0] == ABIS_OM_MDISC_FOM)
233 msg->data[0] = ABIS_OM_MDISC_FOM | 0x01;
234#endif
235 break;
236 case E1INP_SIGN_RSL:
237 proto = IPAC_PROTO_RSL;
238 break;
239 default:
240 msgb_free(msg);
241 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
242 return -EINVAL;
243 }
244
245 msg->l2h = msg->data;
246 ipaccess_prepend_header(msg, sign_link->tei);
247
248 DEBUGP(DMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
249
250 ret = send(bfd->fd, msg->data, msg->len, 0);
251 msgb_free(msg);
252
253 /* set tx delay timer for next event */
254 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
255 e1i_ts->sign.tx_timer.data = e1i_ts;
256
257 /* Reducing this might break the nanoBTS 900 init. */
258 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
259
260 return ret;
261}
262
263/* callback from select.c in case one of the fd's can be read/written */
264static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what)
265{
266 struct e1inp_line *line = bfd->data;
267 unsigned int ts_nr = bfd->priv_nr;
268 unsigned int idx = ts_nr-1;
269 struct e1inp_ts *e1i_ts;
270 int rc = 0;
271
272 /* In case of early RSL we might not yet have a line */
273
274 if (line)
275 e1i_ts = &line->ts[idx];
276
277 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
278 if (what & BSC_FD_READ)
279 rc = handle_ts1_read(bfd);
280 if (what & BSC_FD_WRITE)
281 rc = handle_ts1_write(bfd);
282 } else
283 LOGP(DINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
284
285 return rc;
286}
287
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200288static int hsl_line_update(struct e1inp_line *line,
289 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200290
291struct e1inp_driver hsl_driver = {
292 .name = "hsl",
293 .want_write = ts_want_write,
294 .line_update = hsl_line_update,
295 .default_delay = 0,
296};
297
298/* callback of the OML listening filedescriptor */
299static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
300{
301 int ret;
302 int idx = 0;
303 int i;
304 struct e1inp_line *line = listen_bfd->data;
305 struct e1inp_ts *e1i_ts;
306 struct osmo_fd *bfd;
307 struct sockaddr_in sa;
308 socklen_t sa_len = sizeof(sa);
309
310 if (!(what & BSC_FD_READ))
311 return 0;
312
313 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
314 if (ret < 0) {
315 perror("accept");
316 return ret;
317 }
318 LOGP(DINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n",
319 inet_ntoa(sa.sin_addr));
320
321 /* create virrtual E1 timeslots for signalling */
322 e1inp_ts_config_sign(&line->ts[1-1], line);
323
324 /* initialize the fds */
325 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
326 line->ts[i].driver.ipaccess.fd.fd = -1;
327
328 e1i_ts = &line->ts[idx];
329
330 bfd = &e1i_ts->driver.ipaccess.fd;
331 bfd->fd = ret;
332 bfd->data = line;
333 bfd->priv_nr = PRIV_OML;
334 bfd->cb = hsl_fd_cb;
335 bfd->when = BSC_FD_READ;
336 ret = osmo_fd_register(bfd);
337 if (ret < 0) {
338 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
339 close(bfd->fd);
340 talloc_free(line);
341 return ret;
342 }
343
344 return ret;
345}
346
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200347static int hsl_bts_process(struct ipa_client_link *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200348{
349 /* XXX: not implemented yet. */
350 return 0;
351}
352
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200353static int hsl_line_update(struct e1inp_line *line,
354 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355{
356 int ret = -ENOENT;
357
358 switch(role) {
359 case E1INP_LINE_R_BSC:
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200360 LOGP(DINP, LOGL_NOTICE, "enabling hsl BSC mode\n");
361
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200362 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200363 addr, HSL_TCP_PORT, OSMO_SOCK_F_BIND);
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200364 if (ret < 0)
365 return ret;
366
367 e1h->listen_fd.fd = ret;
368 e1h->listen_fd.when |= BSC_FD_READ;
369 e1h->listen_fd.cb = listen_fd_cb;
370 e1h->listen_fd.data = line;
371
372 if (osmo_fd_register(&e1h->listen_fd) < 0) {
373 close(ret);
374 return ret;
375 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200376 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200377 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusoc07a8e72011-06-21 19:50:04 +0200378 struct ipa_client_link *link;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200379
380 LOGP(DINP, LOGL_NOTICE, "enabling hsl BTS mode\n");
381
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200382 link = ipa_client_link_create(tall_hsl_ctx,
383 &line->ts[0], "hsl", 0,
384 addr, HSL_TCP_PORT,
385 hsl_bts_process, NULL,
Pablo Neira Ayusoe009f4a2011-06-23 13:36:34 +0200386 NULL);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200387 if (link == NULL) {
388 LOGP(DINP, LOGL_ERROR, "cannot create BTS link: %s\n",
389 strerror(errno));
390 return -ENOMEM;
391 }
392 if (ipa_client_link_open(link) < 0) {
393 LOGP(DINP, LOGL_ERROR, "cannot open BTS link: %s\n",
394 strerror(errno));
395 ipa_client_link_close(link);
396 ipa_client_link_destroy(link);
397 return -EIO;
398 }
399 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200400 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200401 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200402 default:
403 break;
404 }
405 return ret;
406}
407
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200408void e1inp_hsl_init(void)
409{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200410 tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl");
411
412 e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200413 if (!e1h)
414 return;
415
416 e1inp_driver_register(&hsl_driver);
417}