Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 1 | /* OpenBSC Abis receive lapd over a unix socket */ |
| 2 | |
Harald Welte | 323d39d | 2017-11-13 01:09:21 +0900 | [diff] [blame] | 3 | /* (C) 2016 by sysmocom - s.f.m.c. GmbH |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 4 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 5 | * Based on other e1_input drivers. |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | 323d39d | 2017-11-13 01:09:21 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 11 | * 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 Sperling | b24efa5 | 2018-08-28 14:03:41 +0200 | [diff] [blame] | 31 | #include <sys/un.h> |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 32 | #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 | |
| 46 | void *tall_unixsocket_ctx; |
| 47 | #define UNIXSOCKET_ALLOC_SIZE 1600 |
| 48 | #define UNIXSOCKET_SOCK_PATH_DEFAULT "/tmp/osmo_abis_line_" |
| 49 | |
| 50 | struct unixsocket_line { |
| 51 | struct osmo_fd fd; |
| 52 | }; |
| 53 | |
| 54 | static int unixsocket_line_update(struct e1inp_line *line); |
| 55 | static int ts_want_write(struct e1inp_ts *e1i_ts); |
| 56 | |
| 57 | static int unixsocket_exception_cb(struct osmo_fd *bfd) |
| 58 | { |
| 59 | struct e1inp_line *line = bfd->data; |
| 60 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 61 | LOGPIL(line, DLINP, LOGL_ERROR, "Socket connection failure, reconnecting... (line=%p, fd=%d)\n", |
| 62 | line, bfd->fd); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 63 | |
| 64 | /* Unregister faulty file descriptor from select loop */ |
| 65 | if(osmo_fd_is_registered(bfd)) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 66 | LOGPIL(line, DLINP, LOGL_DEBUG, "removing inactive socket from select loop... (line=%p, fd=%d)\n", |
| 67 | line, bfd->fd); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 68 | 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 | |
| 79 | static 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 Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 99 | LOGPIL(line, DLMI, LOGL_ERROR, "received to small packet: %d < 2", ret); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 100 | ret = -1; |
| 101 | goto fail; |
| 102 | } |
| 103 | msgb_put(msg, ret); |
| 104 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 105 | LOGPIL(line, DLMI, LOGL_DEBUG, "rx msg: %s (fd=%d)\n", osmo_hexdump_nospc(msg->data, msg->len), bfd->fd); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 106 | |
| 107 | /* check version header */ |
| 108 | version = msgb_pull_u8(msg); |
| 109 | controldata = msgb_pull_u8(msg); |
| 110 | |
| 111 | if (version != UNIXSOCKET_PROTO_VERSION) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 112 | LOGPIL(line, DLMI, LOGL_ERROR, "received message with invalid version %d. valid: %d", |
| 113 | ret, UNIXSOCKET_PROTO_VERSION); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 114 | 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 Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 122 | LOGPIL(line, DLMI, LOGL_ERROR, "received (invalid) control message."); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 123 | ret = -1; |
| 124 | break; |
| 125 | default: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 126 | LOGPIL(line, DLMI, LOGL_ERROR, "received invalid message."); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 127 | ret = -1; |
| 128 | break; |
| 129 | } |
| 130 | fail: |
| 131 | msgb_free(msg); |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | static 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 | |
| 143 | static 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 Welte | de3959e | 2020-10-18 22:28:50 +0200 | [diff] [blame] | 150 | bfd->when &= ~OSMO_FD_WRITE; |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 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 Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 156 | LOGPITS(e1i_ts, DLINP, LOGL_INFO, "no message available (line=%p)\n", line); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* set tx delay timer for next event */ |
Pablo Neira Ayuso | b26f2fd | 2017-06-07 18:32:13 +0200 | [diff] [blame] | 161 | osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 162 | |
| 163 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay); |
| 164 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 165 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "sending: %s (line=%p)\n", msgb_hexdump(msg), line); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 166 | lapd_transmit(e1i_ts->lapd, sign_link->tei, |
| 167 | sign_link->sapi, msg); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what) |
| 173 | { |
| 174 | int ret = 0; |
| 175 | |
Harald Welte | de3959e | 2020-10-18 22:28:50 +0200 | [diff] [blame] | 176 | if (what & OSMO_FD_READ) |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 177 | ret = unixsocket_read_cb(bfd); |
Harald Welte | de3959e | 2020-10-18 22:28:50 +0200 | [diff] [blame] | 178 | if (what & OSMO_FD_WRITE) |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 179 | ret = unixsocket_write_cb(bfd); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 185 | { |
| 186 | struct unixsocket_line *line = e1i_ts->line->driver_data; |
| 187 | |
Harald Welte | de3959e | 2020-10-18 22:28:50 +0200 | [diff] [blame] | 188 | line->fd.when |= OSMO_FD_WRITE; |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 193 | static void unixsocket_write_msg(struct msgb *msg, struct osmo_fd *bfd) |
| 194 | { |
| 195 | struct e1inp_line *line = bfd->data; |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 196 | int ret; |
| 197 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 198 | LOGPIL(line, DLMI, LOGL_DEBUG, "tx msg: %s (fd=%d)\n", |
| 199 | osmo_hexdump_nospc(msg->data, msg->len), bfd->fd); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 200 | |
| 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 Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 206 | LOGPIL(line, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /*! |
| 210 | * \brief unixsocket_write_msg lapd callback for data to unixsocket |
| 211 | * \param msg |
| 212 | * \param cbdata |
| 213 | */ |
| 214 | static 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 | |
| 226 | static int unixsocket_line_update(struct e1inp_line *line) |
| 227 | { |
| 228 | struct unixsocket_line *config; |
Stefan Sperling | 0d7d0b0 | 2018-09-20 17:34:31 +0200 | [diff] [blame] | 229 | struct sockaddr_un un; |
Stefan Sperling | b24efa5 | 2018-08-28 14:03:41 +0200 | [diff] [blame] | 230 | const char *sock_path; |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 231 | int ret = 0; |
| 232 | int i; |
| 233 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 234 | LOGPIL(line, DLINP, LOGL_NOTICE, "line update (line=%p)\n", line); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 235 | |
| 236 | if (!line->driver_data) |
| 237 | line->driver_data = talloc_zero(line, struct unixsocket_line); |
| 238 | |
| 239 | if (!line->driver_data) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 240 | LOGPIL(line, DLINP, LOGL_ERROR, "OOM in line update (line=%p)\n", line); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 241 | return -ENOMEM; |
| 242 | } |
| 243 | |
| 244 | config = line->driver_data; |
| 245 | config->fd.data = line; |
Harald Welte | de3959e | 2020-10-18 22:28:50 +0200 | [diff] [blame] | 246 | config->fd.when = OSMO_FD_READ; |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 247 | config->fd.cb = unixsocket_cb; |
| 248 | |
| 249 | /* Open unix domain socket */ |
Stefan Sperling | b24efa5 | 2018-08-28 14:03:41 +0200 | [diff] [blame] | 250 | if (line->sock_path == NULL) { |
Stefan Sperling | 0d7d0b0 | 2018-09-20 17:34:31 +0200 | [diff] [blame] | 251 | ret = snprintf(un.sun_path, sizeof(un.sun_path), "%s%d", |
| 252 | UNIXSOCKET_SOCK_PATH_DEFAULT, line->num); |
| 253 | if (ret == -1) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 254 | LOGPIL(line, DLINP, LOGL_ERROR, "Cannot create default socket path: %s\n", strerror(errno)); |
Stefan Sperling | 0d7d0b0 | 2018-09-20 17:34:31 +0200 | [diff] [blame] | 255 | return -errno; |
| 256 | } else if (ret >= sizeof(un.sun_path)) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 257 | LOGPIL(line, DLINP, LOGL_ERROR, "Default socket path exceeds %zd bytes: %s%d\n", |
Stefan Sperling | 0d7d0b0 | 2018-09-20 17:34:31 +0200 | [diff] [blame] | 258 | sizeof(un.sun_path), UNIXSOCKET_SOCK_PATH_DEFAULT, line->num); |
| 259 | return -ENOSPC; |
| 260 | } |
| 261 | sock_path = un.sun_path; |
Stefan Sperling | b24efa5 | 2018-08-28 14:03:41 +0200 | [diff] [blame] | 262 | } else |
| 263 | sock_path = line->sock_path; |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 264 | 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 Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 271 | LOGPIL(line, DLINP, LOGL_ERROR, "unable to open socket: %s (line=%p, fd=%d)\n", sock_path, |
| 272 | line, config->fd.fd); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 273 | return ret; |
| 274 | } |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 275 | 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 Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 277 | config->fd.fd = ret; |
| 278 | |
| 279 | /* Register socket in select loop */ |
| 280 | if (osmo_fd_register(&config->fd) < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 281 | LOGPIL(line, DLINP, LOGL_ERROR, "error registering new socket (line=%p, fd=%d)\n", |
| 282 | line, config->fd.fd); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 283 | 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]; |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 290 | char name[32]; |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 291 | if (!e1i_ts->lapd) { |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 292 | e1inp_ts_name(name, sizeof(name), e1i_ts); |
| 293 | e1i_ts->lapd = lapd_instance_alloc2(1, |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 294 | unixsocket_write_msg_lapd_cb, &config->fd, |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 295 | e1inp_dlsap_up, e1i_ts, &lapd_profile_abis, name); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | /* Ensure ericsson-superchannel is turned of when |
| 300 | * a new connection is made */ |
| 301 | e1inp_ericsson_set_altc(line, 0); |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | struct e1inp_driver unixsocket_driver = { |
| 307 | .name = "unixsocket", |
| 308 | .want_write = ts_want_write, |
| 309 | .line_update = unixsocket_line_update, |
| 310 | .default_delay = 0, |
| 311 | }; |
| 312 | |
| 313 | void e1inp_unixsocket_init(void) |
| 314 | { |
| 315 | tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket"); |
| 316 | e1inp_driver_register(&unixsocket_driver); |
| 317 | } |
| 318 | |
| 319 | void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel) |
| 320 | { |
| 321 | struct unixsocket_line *config; |
| 322 | struct msgb *msg; |
| 323 | |
| 324 | if (!unixline) |
| 325 | return; |
| 326 | |
| 327 | if (unixline->driver != &unixsocket_driver) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 328 | LOGPIL(unixline, DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n"); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | config = unixline->driver_data; |
| 333 | if (!config) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 334 | LOGPIL(unixline, DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n"); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | msg = msgb_alloc_headroom(200, 100, "ALTC"); |
| 340 | |
| 341 | /* version header */ |
| 342 | msgb_put_u8(msg, UNIXSOCKET_PROTO_VERSION); |
| 343 | /* data|control */ |
| 344 | msgb_put_u8(msg, UNIXSOCKET_PROTO_CONTROL); |
| 345 | |
| 346 | /* magic */ |
| 347 | msgb_put_u32(msg, 0x23004200); |
| 348 | msgb_put_u8(msg, superchannel ? 1 : 0); |
| 349 | |
| 350 | unixsocket_write_msg(msg, &config->fd); |
| 351 | } |
| 352 | |