blob: 595dc7d4f658b8c3ddff9387eb845faa0f2dff28 [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
Harald Welteb5af0992020-01-12 12:59:52 +010061 LOGPIL(line, DLINP, LOGL_ERROR, "Socket connection failure, reconnecting... (line=%p, fd=%d)\n",
62 line, bfd->fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +010063
64 /* Unregister faulty file descriptor from select loop */
65 if(osmo_fd_is_registered(bfd)) {
Harald Welteb5af0992020-01-12 12:59:52 +010066 LOGPIL(line, DLINP, LOGL_DEBUG, "removing inactive socket from select loop... (line=%p, fd=%d)\n",
67 line, bfd->fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +010068 osmo_fd_unregister(bfd);
69 }
70
71 /* Close faulty file descriptor */
72 close(bfd->fd);
73
74 unixsocket_line_update(line);
75
76 return 0;
77}
78
79static int unixsocket_read_cb(struct osmo_fd *bfd)
80{
81 struct e1inp_line *line = bfd->data;
82 struct msgb *msg = msgb_alloc(UNIXSOCKET_ALLOC_SIZE, "UNIXSOCKET TS");
83 uint8_t version;
84 uint8_t controldata;
85 int ret;
86
87 if (!msg)
88 return -ENOMEM;
89
90 ret = read(bfd->fd, msg->data, UNIXSOCKET_ALLOC_SIZE - 16);
91 if (ret == 0) {
92 unixsocket_exception_cb(bfd);
93 goto fail;
94 } else if (ret < 0) {
95 perror("read ");
96 goto fail;
97 } else if (ret < 2) {
98 /* packet must be at least 2 byte long to hold version + control/data header */
Harald Welteb5af0992020-01-12 12:59:52 +010099 LOGPIL(line, DLMI, LOGL_ERROR, "received to small packet: %d < 2", ret);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100100 ret = -1;
101 goto fail;
102 }
103 msgb_put(msg, ret);
104
Harald Welteb5af0992020-01-12 12:59:52 +0100105 LOGPIL(line, DLMI, LOGL_DEBUG, "rx msg: %s (fd=%d)\n", osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100106
107 /* check version header */
108 version = msgb_pull_u8(msg);
109 controldata = msgb_pull_u8(msg);
110
111 if (version != UNIXSOCKET_PROTO_VERSION) {
Harald Welteb5af0992020-01-12 12:59:52 +0100112 LOGPIL(line, DLMI, LOGL_ERROR, "received message with invalid version %d. valid: %d",
113 ret, UNIXSOCKET_PROTO_VERSION);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100114 ret = -1;
115 goto fail;
116 }
117
118 switch (controldata) {
119 case UNIXSOCKET_PROTO_DATA:
120 return e1inp_rx_ts_lapd(&line->ts[0], msg);
121 case UNIXSOCKET_PROTO_CONTROL:
Harald Welteb5af0992020-01-12 12:59:52 +0100122 LOGPIL(line, DLMI, LOGL_ERROR, "received (invalid) control message.");
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100123 ret = -1;
124 break;
125 default:
Harald Welteb5af0992020-01-12 12:59:52 +0100126 LOGPIL(line, DLMI, LOGL_ERROR, "received invalid message.");
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100127 ret = -1;
128 break;
129 }
130fail:
131 msgb_free(msg);
132 return ret;
133}
134
135static void timeout_ts1_write(void *data)
136{
137 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
138
139 /* trigger write of ts1, due to tx delay timer */
140 ts_want_write(e1i_ts);
141}
142
143static int unixsocket_write_cb(struct osmo_fd *bfd)
144{
145 struct e1inp_line *line = bfd->data;
146 struct e1inp_ts *e1i_ts = &line->ts[0];
147 struct msgb *msg;
148 struct e1inp_sign_link *sign_link;
149
Harald Welte949b8a22020-10-18 23:01:53 +0200150 osmo_fd_write_disable(bfd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100151
152 /* get the next msg for this timeslot */
153 msg = e1inp_tx_ts(e1i_ts, &sign_link);
154 if (!msg) {
155 /* no message after tx delay timer */
Harald Welteb5af0992020-01-12 12:59:52 +0100156 LOGPITS(e1i_ts, DLINP, LOGL_INFO, "no message available (line=%p)\n", line);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100157 return 0;
158 }
159
160 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200161 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100162
163 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
164
Harald Welteb5af0992020-01-12 12:59:52 +0100165 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "sending: %s (line=%p)\n", msgb_hexdump(msg), line);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100166 lapd_transmit(e1i_ts->lapd, sign_link->tei,
167 sign_link->sapi, msg);
168
169 return 0;
170}
171
172static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what)
173{
174 int ret = 0;
175
Harald Weltede3959e2020-10-18 22:28:50 +0200176 if (what & OSMO_FD_READ)
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100177 ret = unixsocket_read_cb(bfd);
Harald Weltede3959e2020-10-18 22:28:50 +0200178 if (what & OSMO_FD_WRITE)
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100179 ret = unixsocket_write_cb(bfd);
180
181 return ret;
182}
183
184static int ts_want_write(struct e1inp_ts *e1i_ts)
185{
186 struct unixsocket_line *line = e1i_ts->line->driver_data;
187
Harald Welte949b8a22020-10-18 23:01:53 +0200188 osmo_fd_write_enable(&line->fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100189
190 return 0;
191}
192
Harald Welteb5af0992020-01-12 12:59:52 +0100193static void unixsocket_write_msg(struct msgb *msg, struct osmo_fd *bfd)
194{
195 struct e1inp_line *line = bfd->data;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100196 int ret;
197
Harald Welteb5af0992020-01-12 12:59:52 +0100198 LOGPIL(line, DLMI, LOGL_DEBUG, "tx msg: %s (fd=%d)\n",
199 osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100200
201 ret = write(bfd->fd, msg->data, msg->len);
202 msgb_free(msg);
203 if (ret == -1)
204 unixsocket_exception_cb(bfd);
205 else if (ret < 0)
Harald Welteb5af0992020-01-12 12:59:52 +0100206 LOGPIL(line, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100207}
208
209/*!
210 * \brief unixsocket_write_msg lapd callback for data to unixsocket
211 * \param msg
212 * \param cbdata
213 */
214static void unixsocket_write_msg_lapd_cb(struct msgb *msg, void *cbdata)
215{
216 struct osmo_fd *bfd = cbdata;
217
218 /* data|control */
219 msgb_push_u8(msg, UNIXSOCKET_PROTO_DATA);
220 /* add version header */
221 msgb_push_u8(msg, UNIXSOCKET_PROTO_VERSION);
222
223 unixsocket_write_msg(msg, bfd);
224}
225
226static int unixsocket_line_update(struct e1inp_line *line)
227{
228 struct unixsocket_line *config;
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200229 struct sockaddr_un un;
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200230 const char *sock_path;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100231 int ret = 0;
232 int i;
233
Harald Welteb5af0992020-01-12 12:59:52 +0100234 LOGPIL(line, DLINP, LOGL_NOTICE, "line update (line=%p)\n", line);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100235
236 if (!line->driver_data)
237 line->driver_data = talloc_zero(line, struct unixsocket_line);
238
239 if (!line->driver_data) {
Harald Welteb5af0992020-01-12 12:59:52 +0100240 LOGPIL(line, DLINP, LOGL_ERROR, "OOM in line update (line=%p)\n", line);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100241 return -ENOMEM;
242 }
243
244 config = line->driver_data;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100245
246 /* Open unix domain socket */
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200247 if (line->sock_path == NULL) {
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200248 ret = snprintf(un.sun_path, sizeof(un.sun_path), "%s%d",
249 UNIXSOCKET_SOCK_PATH_DEFAULT, line->num);
250 if (ret == -1) {
Harald Welteb5af0992020-01-12 12:59:52 +0100251 LOGPIL(line, DLINP, LOGL_ERROR, "Cannot create default socket path: %s\n", strerror(errno));
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200252 return -errno;
253 } else if (ret >= sizeof(un.sun_path)) {
Harald Welteb5af0992020-01-12 12:59:52 +0100254 LOGPIL(line, DLINP, LOGL_ERROR, "Default socket path exceeds %zd bytes: %s%d\n",
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200255 sizeof(un.sun_path), UNIXSOCKET_SOCK_PATH_DEFAULT, line->num);
256 return -ENOSPC;
257 }
258 sock_path = un.sun_path;
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200259 } else
260 sock_path = line->sock_path;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100261 ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
262 OSMO_SOCK_F_CONNECT);
263 if (ret < 0) {
264 /* Note: We will not free the allocated driver_data memory if
265 * opening the socket fails. The caller may want to call this
266 * function multiple times using config->fd.data as line
267 * parameter. Freeing now would destroy that reference. */
Harald Welteb5af0992020-01-12 12:59:52 +0100268 LOGPIL(line, DLINP, LOGL_ERROR, "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
269 line, config->fd.fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100270 return ret;
271 }
Harald Welteb5af0992020-01-12 12:59:52 +0100272 LOGPIL(line, DLINP, LOGL_DEBUG, "successfully opend (new) socket: %s (line=%p, fd=%d, ret=%d)\n",
273 sock_path, line, config->fd.fd, ret);
Harald Welte6e831b72020-10-18 22:59:58 +0200274 osmo_fd_setup(&config->fd, ret, OSMO_FD_READ, unixsocket_cb, line, 0);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100275
276 /* Register socket in select loop */
277 if (osmo_fd_register(&config->fd) < 0) {
Harald Welteb5af0992020-01-12 12:59:52 +0100278 LOGPIL(line, DLINP, LOGL_ERROR, "error registering new socket (line=%p, fd=%d)\n",
279 line, config->fd.fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100280 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];
Harald Welteb9031882020-05-02 21:09:15 +0200287 char name[32];
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100288 if (!e1i_ts->lapd) {
Harald Welteb9031882020-05-02 21:09:15 +0200289 e1inp_ts_name(name, sizeof(name), e1i_ts);
290 e1i_ts->lapd = lapd_instance_alloc2(1,
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100291 unixsocket_write_msg_lapd_cb, &config->fd,
Harald Welteb9031882020-05-02 21:09:15 +0200292 e1inp_dlsap_up, e1i_ts, &lapd_profile_abis, name);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100293 }
294 }
295
296 /* Ensure ericsson-superchannel is turned of when
297 * a new connection is made */
298 e1inp_ericsson_set_altc(line, 0);
299
300 return ret;
301}
302
303struct e1inp_driver unixsocket_driver = {
304 .name = "unixsocket",
305 .want_write = ts_want_write,
306 .line_update = unixsocket_line_update,
307 .default_delay = 0,
308};
309
310void e1inp_unixsocket_init(void)
311{
312 tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
313 e1inp_driver_register(&unixsocket_driver);
314}
315
316void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel)
317{
318 struct unixsocket_line *config;
319 struct msgb *msg;
320
321 if (!unixline)
322 return;
323
324 if (unixline->driver != &unixsocket_driver) {
Harald Welteb5af0992020-01-12 12:59:52 +0100325 LOGPIL(unixline, DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100326 return;
327 }
328
329 config = unixline->driver_data;
330 if (!config) {
Harald Welteb5af0992020-01-12 12:59:52 +0100331 LOGPIL(unixline, DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100332 return;
333 }
334
335
336 msg = msgb_alloc_headroom(200, 100, "ALTC");
337
338 /* version header */
339 msgb_put_u8(msg, UNIXSOCKET_PROTO_VERSION);
340 /* data|control */
341 msgb_put_u8(msg, UNIXSOCKET_PROTO_CONTROL);
342
343 /* magic */
344 msgb_put_u32(msg, 0x23004200);
345 msgb_put_u8(msg, superchannel ? 1 : 0);
346
347 unixsocket_write_msg(msg, &config->fd);
348}
349