blob: bc4b3570c816dad64115b266afe78879d47542ff [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>
Stefan Sperlingb24efa52018-08-28 14:03:41 +020031#include <sys/un.h>
Alexander Couzensbeb10ef2016-11-01 22:05:13 +010032#include <limits.h>
33#include <string.h>
34
35#include <osmocom/core/talloc.h>
36#include <osmocom/core/logging.h>
37#include <osmocom/core/socket.h>
38
39#include <osmocom/abis/e1_input.h>
40#include <osmocom/abis/lapd.h>
41#include <osmocom/abis/e1_input.h>
42
43#include <osmocom/abis/unixsocket_proto.h>
44#include "internal.h"
45
46void *tall_unixsocket_ctx;
47#define UNIXSOCKET_ALLOC_SIZE 1600
48#define UNIXSOCKET_SOCK_PATH_DEFAULT "/tmp/osmo_abis_line_"
49
50struct unixsocket_line {
51 struct osmo_fd fd;
52};
53
54static int unixsocket_line_update(struct e1inp_line *line);
55static int ts_want_write(struct e1inp_ts *e1i_ts);
56
57static int unixsocket_exception_cb(struct osmo_fd *bfd)
58{
59 struct e1inp_line *line = bfd->data;
60
61 LOGP(DLINP, LOGL_ERROR,
62 "Socket connection failure, reconnecting... (line=%p, fd=%d)\n",
63 line, bfd->fd);
64
65 /* Unregister faulty file descriptor from select loop */
66 if(osmo_fd_is_registered(bfd)) {
67 LOGP(DLINP, LOGL_DEBUG,
68 "removing inactive socket from select loop... (line=%p, fd=%d)\n",
69 line, bfd->fd);
70 osmo_fd_unregister(bfd);
71 }
72
73 /* Close faulty file descriptor */
74 close(bfd->fd);
75
76 unixsocket_line_update(line);
77
78 return 0;
79}
80
81static int unixsocket_read_cb(struct osmo_fd *bfd)
82{
83 struct e1inp_line *line = bfd->data;
84 struct msgb *msg = msgb_alloc(UNIXSOCKET_ALLOC_SIZE, "UNIXSOCKET TS");
85 uint8_t version;
86 uint8_t controldata;
87 int ret;
88
89 if (!msg)
90 return -ENOMEM;
91
92 ret = read(bfd->fd, msg->data, UNIXSOCKET_ALLOC_SIZE - 16);
93 if (ret == 0) {
94 unixsocket_exception_cb(bfd);
95 goto fail;
96 } else if (ret < 0) {
97 perror("read ");
98 goto fail;
99 } else if (ret < 2) {
100 /* packet must be at least 2 byte long to hold version + control/data header */
101 LOGP(DLMI, LOGL_ERROR, "received to small packet: %d < 2", ret);
102 ret = -1;
103 goto fail;
104 }
105 msgb_put(msg, ret);
106
107 LOGP(DLMI, LOGL_DEBUG, "rx msg: %s (fd=%d)\n",
108 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
109
110 /* check version header */
111 version = msgb_pull_u8(msg);
112 controldata = msgb_pull_u8(msg);
113
114 if (version != UNIXSOCKET_PROTO_VERSION) {
115 LOGP(DLMI, LOGL_ERROR, "received message with invalid version %d. valid: %d",
116 ret, UNIXSOCKET_PROTO_VERSION);
117 ret = -1;
118 goto fail;
119 }
120
121 switch (controldata) {
122 case UNIXSOCKET_PROTO_DATA:
123 return e1inp_rx_ts_lapd(&line->ts[0], msg);
124 case UNIXSOCKET_PROTO_CONTROL:
125 LOGP(DLMI, LOGL_ERROR, "received (invalid) control message.");
126 ret = -1;
127 break;
128 default:
129 LOGP(DLMI, LOGL_ERROR, "received invalid message.");
130 ret = -1;
131 break;
132 }
133fail:
134 msgb_free(msg);
135 return ret;
136}
137
138static void timeout_ts1_write(void *data)
139{
140 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
141
142 /* trigger write of ts1, due to tx delay timer */
143 ts_want_write(e1i_ts);
144}
145
146static int unixsocket_write_cb(struct osmo_fd *bfd)
147{
148 struct e1inp_line *line = bfd->data;
149 struct e1inp_ts *e1i_ts = &line->ts[0];
150 struct msgb *msg;
151 struct e1inp_sign_link *sign_link;
152
153 bfd->when &= ~BSC_FD_WRITE;
154
155 /* get the next msg for this timeslot */
156 msg = e1inp_tx_ts(e1i_ts, &sign_link);
157 if (!msg) {
158 /* no message after tx delay timer */
159 LOGP(DLINP, LOGL_INFO,
160 "no message available (line=%p)\n", line);
161 return 0;
162 }
163
164 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200165 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100166
167 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
168
169 LOGP(DLINP, LOGL_DEBUG, "sending: %s (line=%p)\n",
170 msgb_hexdump(msg), line);
171 lapd_transmit(e1i_ts->lapd, sign_link->tei,
172 sign_link->sapi, msg);
173
174 return 0;
175}
176
177static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what)
178{
179 int ret = 0;
180
181 if (what & BSC_FD_READ)
182 ret = unixsocket_read_cb(bfd);
183 if (what & BSC_FD_WRITE)
184 ret = unixsocket_write_cb(bfd);
185
186 return ret;
187}
188
189static int ts_want_write(struct e1inp_ts *e1i_ts)
190{
191 struct unixsocket_line *line = e1i_ts->line->driver_data;
192
193 line->fd.when |= BSC_FD_WRITE;
194
195 return 0;
196}
197
198static void unixsocket_write_msg(struct msgb *msg, struct osmo_fd *bfd) {
199 int ret;
200
201 LOGP(DLMI, LOGL_DEBUG, "tx msg: %s (fd=%d)\n",
202 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
203
204 ret = write(bfd->fd, msg->data, msg->len);
205 msgb_free(msg);
206 if (ret == -1)
207 unixsocket_exception_cb(bfd);
208 else if (ret < 0)
209 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
210}
211
212/*!
213 * \brief unixsocket_write_msg lapd callback for data to unixsocket
214 * \param msg
215 * \param cbdata
216 */
217static void unixsocket_write_msg_lapd_cb(struct msgb *msg, void *cbdata)
218{
219 struct osmo_fd *bfd = cbdata;
220
221 /* data|control */
222 msgb_push_u8(msg, UNIXSOCKET_PROTO_DATA);
223 /* add version header */
224 msgb_push_u8(msg, UNIXSOCKET_PROTO_VERSION);
225
226 unixsocket_write_msg(msg, bfd);
227}
228
229static int unixsocket_line_update(struct e1inp_line *line)
230{
231 struct unixsocket_line *config;
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200232 struct sockaddr_un un;
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200233 const char *sock_path;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100234 int ret = 0;
235 int i;
236
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100237 LOGP(DLINP, LOGL_NOTICE, "line update (line=%p)\n", line);
238
239 if (!line->driver_data)
240 line->driver_data = talloc_zero(line, struct unixsocket_line);
241
242 if (!line->driver_data) {
243 LOGP(DLINP, LOGL_ERROR,
244 "OOM in line update (line=%p)\n", line);
245 return -ENOMEM;
246 }
247
248 config = line->driver_data;
249 config->fd.data = line;
250 config->fd.when = BSC_FD_READ;
251 config->fd.cb = unixsocket_cb;
252
253 /* Open unix domain socket */
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200254 if (line->sock_path == NULL) {
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200255 ret = snprintf(un.sun_path, sizeof(un.sun_path), "%s%d",
256 UNIXSOCKET_SOCK_PATH_DEFAULT, line->num);
257 if (ret == -1) {
258 LOGP(DLINP, LOGL_ERROR, "Cannot create default socket path: %s\n", strerror(errno));
259 return -errno;
260 } else if (ret >= sizeof(un.sun_path)) {
261 LOGP(DLINP, LOGL_ERROR, "Default socket path exceeds %zd bytes: %s%d\n",
262 sizeof(un.sun_path), UNIXSOCKET_SOCK_PATH_DEFAULT, line->num);
263 return -ENOSPC;
264 }
265 sock_path = un.sun_path;
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200266 } else
267 sock_path = line->sock_path;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100268 ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
269 OSMO_SOCK_F_CONNECT);
270 if (ret < 0) {
271 /* Note: We will not free the allocated driver_data memory if
272 * opening the socket fails. The caller may want to call this
273 * function multiple times using config->fd.data as line
274 * parameter. Freeing now would destroy that reference. */
275 LOGP(DLINP, LOGL_ERROR,
276 "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
277 line, config->fd.fd);
278 return ret;
279 }
280 LOGP(DLINP, LOGL_DEBUG,
281 "successfully opend (new) socket: %s (line=%p, fd=%d, ret=%d)\n",
282 sock_path, line, config->fd.fd, ret);
283 config->fd.fd = ret;
284
285 /* Register socket in select loop */
286 if (osmo_fd_register(&config->fd) < 0) {
287 LOGP(DLINP, LOGL_ERROR,
288 "error registering new socket (line=%p, fd=%d)\n",
289 line, config->fd.fd);
290 close(config->fd.fd);
291 return -EIO;
292 }
293
294 /* Set line parameter */
295 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
296 struct e1inp_ts *e1i_ts = &line->ts[i];
297 if (!e1i_ts->lapd) {
298 e1i_ts->lapd = lapd_instance_alloc(1,
299 unixsocket_write_msg_lapd_cb, &config->fd,
300 e1inp_dlsap_up, e1i_ts, &lapd_profile_abis);
301 }
302 }
303
304 /* Ensure ericsson-superchannel is turned of when
305 * a new connection is made */
306 e1inp_ericsson_set_altc(line, 0);
307
308 return ret;
309}
310
311struct e1inp_driver unixsocket_driver = {
312 .name = "unixsocket",
313 .want_write = ts_want_write,
314 .line_update = unixsocket_line_update,
315 .default_delay = 0,
316};
317
318void e1inp_unixsocket_init(void)
319{
320 tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
321 e1inp_driver_register(&unixsocket_driver);
322}
323
324void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel)
325{
326 struct unixsocket_line *config;
327 struct msgb *msg;
328
329 if (!unixline)
330 return;
331
332 if (unixline->driver != &unixsocket_driver) {
333 LOGP(DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
334 return;
335 }
336
337 config = unixline->driver_data;
338 if (!config) {
339 LOGP(DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
340 return;
341 }
342
343
344 msg = msgb_alloc_headroom(200, 100, "ALTC");
345
346 /* version header */
347 msgb_put_u8(msg, UNIXSOCKET_PROTO_VERSION);
348 /* data|control */
349 msgb_put_u8(msg, UNIXSOCKET_PROTO_CONTROL);
350
351 /* magic */
352 msgb_put_u32(msg, 0x23004200);
353 msgb_put_u8(msg, superchannel ? 1 : 0);
354
355 unixsocket_write_msg(msg, &config->fd);
356}
357