blob: c49928d8d55dc412a544ea6f77663ab4725fa905 [file] [log] [blame]
Alexander Couzensbeb10ef2016-11-01 22:05:13 +01001/* OpenBSC Abis receive lapd over a unix socket */
2
Harald Welte323d39d2017-11-13 01:09:21 +09003/* (C) 2016 by sysmocom - s.f.m.c. GmbH
Alexander Couzensbeb10ef2016-11-01 22:05:13 +01004 * Author: Alexander Couzens <lynxis@fe80.eu>
5 * Based on other e1_input drivers.
6 *
7 * All Rights Reserved
8 *
Harald Welte323d39d2017-11-13 01:09:21 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Alexander Couzensbeb10ef2016-11-01 22:05:13 +010011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26
27#include <errno.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <sys/socket.h>
31#include <limits.h>
32#include <string.h>
33
34#include <osmocom/core/talloc.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/socket.h>
37
38#include <osmocom/abis/e1_input.h>
39#include <osmocom/abis/lapd.h>
40#include <osmocom/abis/e1_input.h>
41
42#include <osmocom/abis/unixsocket_proto.h>
43#include "internal.h"
44
45void *tall_unixsocket_ctx;
46#define UNIXSOCKET_ALLOC_SIZE 1600
47#define UNIXSOCKET_SOCK_PATH_DEFAULT "/tmp/osmo_abis_line_"
48
49struct unixsocket_line {
50 struct osmo_fd fd;
51};
52
53static int unixsocket_line_update(struct e1inp_line *line);
54static int ts_want_write(struct e1inp_ts *e1i_ts);
55
56static int unixsocket_exception_cb(struct osmo_fd *bfd)
57{
58 struct e1inp_line *line = bfd->data;
59
60 LOGP(DLINP, LOGL_ERROR,
61 "Socket connection failure, reconnecting... (line=%p, fd=%d)\n",
62 line, bfd->fd);
63
64 /* Unregister faulty file descriptor from select loop */
65 if(osmo_fd_is_registered(bfd)) {
66 LOGP(DLINP, LOGL_DEBUG,
67 "removing inactive socket from select loop... (line=%p, fd=%d)\n",
68 line, bfd->fd);
69 osmo_fd_unregister(bfd);
70 }
71
72 /* Close faulty file descriptor */
73 close(bfd->fd);
74
75 unixsocket_line_update(line);
76
77 return 0;
78}
79
80static int unixsocket_read_cb(struct osmo_fd *bfd)
81{
82 struct e1inp_line *line = bfd->data;
83 struct msgb *msg = msgb_alloc(UNIXSOCKET_ALLOC_SIZE, "UNIXSOCKET TS");
84 uint8_t version;
85 uint8_t controldata;
86 int ret;
87
88 if (!msg)
89 return -ENOMEM;
90
91 ret = read(bfd->fd, msg->data, UNIXSOCKET_ALLOC_SIZE - 16);
92 if (ret == 0) {
93 unixsocket_exception_cb(bfd);
94 goto fail;
95 } else if (ret < 0) {
96 perror("read ");
97 goto fail;
98 } else if (ret < 2) {
99 /* packet must be at least 2 byte long to hold version + control/data header */
100 LOGP(DLMI, LOGL_ERROR, "received to small packet: %d < 2", ret);
101 ret = -1;
102 goto fail;
103 }
104 msgb_put(msg, ret);
105
106 LOGP(DLMI, LOGL_DEBUG, "rx msg: %s (fd=%d)\n",
107 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
108
109 /* check version header */
110 version = msgb_pull_u8(msg);
111 controldata = msgb_pull_u8(msg);
112
113 if (version != UNIXSOCKET_PROTO_VERSION) {
114 LOGP(DLMI, LOGL_ERROR, "received message with invalid version %d. valid: %d",
115 ret, UNIXSOCKET_PROTO_VERSION);
116 ret = -1;
117 goto fail;
118 }
119
120 switch (controldata) {
121 case UNIXSOCKET_PROTO_DATA:
122 return e1inp_rx_ts_lapd(&line->ts[0], msg);
123 case UNIXSOCKET_PROTO_CONTROL:
124 LOGP(DLMI, LOGL_ERROR, "received (invalid) control message.");
125 ret = -1;
126 break;
127 default:
128 LOGP(DLMI, LOGL_ERROR, "received invalid message.");
129 ret = -1;
130 break;
131 }
132fail:
133 msgb_free(msg);
134 return ret;
135}
136
137static void timeout_ts1_write(void *data)
138{
139 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
140
141 /* trigger write of ts1, due to tx delay timer */
142 ts_want_write(e1i_ts);
143}
144
145static int unixsocket_write_cb(struct osmo_fd *bfd)
146{
147 struct e1inp_line *line = bfd->data;
148 struct e1inp_ts *e1i_ts = &line->ts[0];
149 struct msgb *msg;
150 struct e1inp_sign_link *sign_link;
151
152 bfd->when &= ~BSC_FD_WRITE;
153
154 /* get the next msg for this timeslot */
155 msg = e1inp_tx_ts(e1i_ts, &sign_link);
156 if (!msg) {
157 /* no message after tx delay timer */
158 LOGP(DLINP, LOGL_INFO,
159 "no message available (line=%p)\n", line);
160 return 0;
161 }
162
163 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200164 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100165
166 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
167
168 LOGP(DLINP, LOGL_DEBUG, "sending: %s (line=%p)\n",
169 msgb_hexdump(msg), line);
170 lapd_transmit(e1i_ts->lapd, sign_link->tei,
171 sign_link->sapi, msg);
172
173 return 0;
174}
175
176static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what)
177{
178 int ret = 0;
179
180 if (what & BSC_FD_READ)
181 ret = unixsocket_read_cb(bfd);
182 if (what & BSC_FD_WRITE)
183 ret = unixsocket_write_cb(bfd);
184
185 return ret;
186}
187
188static int ts_want_write(struct e1inp_ts *e1i_ts)
189{
190 struct unixsocket_line *line = e1i_ts->line->driver_data;
191
192 line->fd.when |= BSC_FD_WRITE;
193
194 return 0;
195}
196
197static void unixsocket_write_msg(struct msgb *msg, struct osmo_fd *bfd) {
198 int ret;
199
200 LOGP(DLMI, LOGL_DEBUG, "tx msg: %s (fd=%d)\n",
201 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
202
203 ret = write(bfd->fd, msg->data, msg->len);
204 msgb_free(msg);
205 if (ret == -1)
206 unixsocket_exception_cb(bfd);
207 else if (ret < 0)
208 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
209}
210
211/*!
212 * \brief unixsocket_write_msg lapd callback for data to unixsocket
213 * \param msg
214 * \param cbdata
215 */
216static void unixsocket_write_msg_lapd_cb(struct msgb *msg, void *cbdata)
217{
218 struct osmo_fd *bfd = cbdata;
219
220 /* data|control */
221 msgb_push_u8(msg, UNIXSOCKET_PROTO_DATA);
222 /* add version header */
223 msgb_push_u8(msg, UNIXSOCKET_PROTO_VERSION);
224
225 unixsocket_write_msg(msg, bfd);
226}
227
228static int unixsocket_line_update(struct e1inp_line *line)
229{
230 struct unixsocket_line *config;
231 char sock_path[PATH_MAX];
232 int ret = 0;
233 int i;
234
235 if (line->sock_path)
Alexander Couzens2d4888d2017-03-12 20:39:52 +0100236 osmo_strlcpy(sock_path, line->sock_path, PATH_MAX);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100237 else
238 sprintf(sock_path, "%s%d", UNIXSOCKET_SOCK_PATH_DEFAULT,
239 line->num);
240
241 LOGP(DLINP, LOGL_NOTICE, "line update (line=%p)\n", line);
242
243 if (!line->driver_data)
244 line->driver_data = talloc_zero(line, struct unixsocket_line);
245
246 if (!line->driver_data) {
247 LOGP(DLINP, LOGL_ERROR,
248 "OOM in line update (line=%p)\n", line);
249 return -ENOMEM;
250 }
251
252 config = line->driver_data;
253 config->fd.data = line;
254 config->fd.when = BSC_FD_READ;
255 config->fd.cb = unixsocket_cb;
256
257 /* Open unix domain socket */
258 ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
259 OSMO_SOCK_F_CONNECT);
260 if (ret < 0) {
261 /* Note: We will not free the allocated driver_data memory if
262 * opening the socket fails. The caller may want to call this
263 * function multiple times using config->fd.data as line
264 * parameter. Freeing now would destroy that reference. */
265 LOGP(DLINP, LOGL_ERROR,
266 "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
267 line, config->fd.fd);
268 return ret;
269 }
270 LOGP(DLINP, LOGL_DEBUG,
271 "successfully opend (new) socket: %s (line=%p, fd=%d, ret=%d)\n",
272 sock_path, line, config->fd.fd, ret);
273 config->fd.fd = ret;
274
275 /* Register socket in select loop */
276 if (osmo_fd_register(&config->fd) < 0) {
277 LOGP(DLINP, LOGL_ERROR,
278 "error registering new socket (line=%p, fd=%d)\n",
279 line, config->fd.fd);
280 close(config->fd.fd);
281 return -EIO;
282 }
283
284 /* Set line parameter */
285 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
286 struct e1inp_ts *e1i_ts = &line->ts[i];
287 if (!e1i_ts->lapd) {
288 e1i_ts->lapd = lapd_instance_alloc(1,
289 unixsocket_write_msg_lapd_cb, &config->fd,
290 e1inp_dlsap_up, e1i_ts, &lapd_profile_abis);
291 }
292 }
293
294 /* Ensure ericsson-superchannel is turned of when
295 * a new connection is made */
296 e1inp_ericsson_set_altc(line, 0);
297
298 return ret;
299}
300
301struct e1inp_driver unixsocket_driver = {
302 .name = "unixsocket",
303 .want_write = ts_want_write,
304 .line_update = unixsocket_line_update,
305 .default_delay = 0,
306};
307
308void e1inp_unixsocket_init(void)
309{
310 tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
311 e1inp_driver_register(&unixsocket_driver);
312}
313
314void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel)
315{
316 struct unixsocket_line *config;
317 struct msgb *msg;
318
319 if (!unixline)
320 return;
321
322 if (unixline->driver != &unixsocket_driver) {
323 LOGP(DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
324 return;
325 }
326
327 config = unixline->driver_data;
328 if (!config) {
329 LOGP(DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
330 return;
331 }
332
333
334 msg = msgb_alloc_headroom(200, 100, "ALTC");
335
336 /* version header */
337 msgb_put_u8(msg, UNIXSOCKET_PROTO_VERSION);
338 /* data|control */
339 msgb_put_u8(msg, UNIXSOCKET_PROTO_CONTROL);
340
341 /* magic */
342 msgb_put_u32(msg, 0x23004200);
343 msgb_put_u8(msg, superchannel ? 1 : 0);
344
345 unixsocket_write_msg(msg, &config->fd);
346}
347