blob: 1d25ddf300ce868ccd5c90e3c005bda907f6ed29 [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
150 bfd->when &= ~BSC_FD_WRITE;
151
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
176 if (what & BSC_FD_READ)
177 ret = unixsocket_read_cb(bfd);
178 if (what & BSC_FD_WRITE)
179 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
188 line->fd.when |= BSC_FD_WRITE;
189
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;
245 config->fd.data = line;
246 config->fd.when = BSC_FD_READ;
247 config->fd.cb = unixsocket_cb;
248
249 /* Open unix domain socket */
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200250 if (line->sock_path == NULL) {
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200251 ret = snprintf(un.sun_path, sizeof(un.sun_path), "%s%d",
252 UNIXSOCKET_SOCK_PATH_DEFAULT, line->num);
253 if (ret == -1) {
Harald Welteb5af0992020-01-12 12:59:52 +0100254 LOGPIL(line, DLINP, LOGL_ERROR, "Cannot create default socket path: %s\n", strerror(errno));
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200255 return -errno;
256 } else if (ret >= sizeof(un.sun_path)) {
Harald Welteb5af0992020-01-12 12:59:52 +0100257 LOGPIL(line, DLINP, LOGL_ERROR, "Default socket path exceeds %zd bytes: %s%d\n",
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200258 sizeof(un.sun_path), UNIXSOCKET_SOCK_PATH_DEFAULT, line->num);
259 return -ENOSPC;
260 }
261 sock_path = un.sun_path;
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200262 } else
263 sock_path = line->sock_path;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100264 ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
265 OSMO_SOCK_F_CONNECT);
266 if (ret < 0) {
267 /* Note: We will not free the allocated driver_data memory if
268 * opening the socket fails. The caller may want to call this
269 * function multiple times using config->fd.data as line
270 * parameter. Freeing now would destroy that reference. */
Harald Welteb5af0992020-01-12 12:59:52 +0100271 LOGPIL(line, DLINP, LOGL_ERROR, "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
272 line, config->fd.fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100273 return ret;
274 }
Harald Welteb5af0992020-01-12 12:59:52 +0100275 LOGPIL(line, DLINP, LOGL_DEBUG, "successfully opend (new) socket: %s (line=%p, fd=%d, ret=%d)\n",
276 sock_path, line, config->fd.fd, ret);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100277 config->fd.fd = ret;
278
279 /* Register socket in select loop */
280 if (osmo_fd_register(&config->fd) < 0) {
Harald Welteb5af0992020-01-12 12:59:52 +0100281 LOGPIL(line, DLINP, LOGL_ERROR, "error registering new socket (line=%p, fd=%d)\n",
282 line, config->fd.fd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100283 close(config->fd.fd);
284 return -EIO;
285 }
286
287 /* Set line parameter */
288 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
289 struct e1inp_ts *e1i_ts = &line->ts[i];
290 if (!e1i_ts->lapd) {
291 e1i_ts->lapd = lapd_instance_alloc(1,
292 unixsocket_write_msg_lapd_cb, &config->fd,
293 e1inp_dlsap_up, e1i_ts, &lapd_profile_abis);
294 }
295 }
296
297 /* Ensure ericsson-superchannel is turned of when
298 * a new connection is made */
299 e1inp_ericsson_set_altc(line, 0);
300
301 return ret;
302}
303
304struct e1inp_driver unixsocket_driver = {
305 .name = "unixsocket",
306 .want_write = ts_want_write,
307 .line_update = unixsocket_line_update,
308 .default_delay = 0,
309};
310
311void e1inp_unixsocket_init(void)
312{
313 tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
314 e1inp_driver_register(&unixsocket_driver);
315}
316
317void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel)
318{
319 struct unixsocket_line *config;
320 struct msgb *msg;
321
322 if (!unixline)
323 return;
324
325 if (unixline->driver != &unixsocket_driver) {
Harald Welteb5af0992020-01-12 12:59:52 +0100326 LOGPIL(unixline, DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100327 return;
328 }
329
330 config = unixline->driver_data;
331 if (!config) {
Harald Welteb5af0992020-01-12 12:59:52 +0100332 LOGPIL(unixline, DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100333 return;
334 }
335
336
337 msg = msgb_alloc_headroom(200, 100, "ALTC");
338
339 /* version header */
340 msgb_put_u8(msg, UNIXSOCKET_PROTO_VERSION);
341 /* data|control */
342 msgb_put_u8(msg, UNIXSOCKET_PROTO_CONTROL);
343
344 /* magic */
345 msgb_put_u32(msg, 0x23004200);
346 msgb_put_u8(msg, superchannel ? 1 : 0);
347
348 unixsocket_write_msg(msg, &config->fd);
349}
350