blob: 00e1f9b398b80c3222376d6148d91cdbead99d8c [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 Sperlingb24efa52018-08-28 14:03:41 +0200232 char default_sock_path[sizeof(struct sockaddr_un) + 1]; /* see unix(7) man page */
233 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) {
255 snprintf(default_sock_path, sizeof(default_sock_path), "%s%d",
256 UNIXSOCKET_SOCK_PATH_DEFAULT, line->num);
257 sock_path = default_sock_path;
258 } else
259 sock_path = line->sock_path;
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100260 ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
261 OSMO_SOCK_F_CONNECT);
262 if (ret < 0) {
263 /* Note: We will not free the allocated driver_data memory if
264 * opening the socket fails. The caller may want to call this
265 * function multiple times using config->fd.data as line
266 * parameter. Freeing now would destroy that reference. */
267 LOGP(DLINP, LOGL_ERROR,
268 "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
269 line, config->fd.fd);
270 return ret;
271 }
272 LOGP(DLINP, LOGL_DEBUG,
273 "successfully opend (new) socket: %s (line=%p, fd=%d, ret=%d)\n",
274 sock_path, line, config->fd.fd, ret);
275 config->fd.fd = ret;
276
277 /* Register socket in select loop */
278 if (osmo_fd_register(&config->fd) < 0) {
279 LOGP(DLINP, LOGL_ERROR,
280 "error registering new socket (line=%p, fd=%d)\n",
281 line, config->fd.fd);
282 close(config->fd.fd);
283 return -EIO;
284 }
285
286 /* Set line parameter */
287 for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
288 struct e1inp_ts *e1i_ts = &line->ts[i];
289 if (!e1i_ts->lapd) {
290 e1i_ts->lapd = lapd_instance_alloc(1,
291 unixsocket_write_msg_lapd_cb, &config->fd,
292 e1inp_dlsap_up, e1i_ts, &lapd_profile_abis);
293 }
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) {
325 LOGP(DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
326 return;
327 }
328
329 config = unixline->driver_data;
330 if (!config) {
331 LOGP(DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
332 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