2010-05-22 10:46:20 +00:00
|
|
|
/* Layer 1 socket interface of layer2/3 stack */
|
|
|
|
|
|
|
|
/* (C) 2010 by Holger Hans Peter Freyther
|
2018-09-06 13:09:28 +00:00
|
|
|
* (C) 2010,2018 by Harald Welte <laforge@gnumonks.org>
|
2010-05-22 10:46:20 +00:00
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-07-27 18:44:46 +00:00
|
|
|
#include <osmocom/bb/common/osmocom_data.h>
|
|
|
|
#include <osmocom/bb/common/l1ctl.h>
|
|
|
|
#include <osmocom/bb/common/logging.h>
|
2010-11-14 11:50:14 +00:00
|
|
|
#include <osmocom/bb/common/l1l2_interface.h>
|
2010-05-22 10:46:20 +00:00
|
|
|
|
2011-04-26 00:55:30 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
2018-09-06 13:09:28 +00:00
|
|
|
#include <osmocom/core/socket.h>
|
2010-10-02 18:39:29 +00:00
|
|
|
|
2010-05-22 10:46:20 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2010-05-24 00:21:24 +00:00
|
|
|
#include <stdlib.h>
|
2010-05-22 10:46:20 +00:00
|
|
|
|
|
|
|
#define GSM_L2_LENGTH 256
|
2010-07-25 08:16:46 +00:00
|
|
|
#define GSM_L2_HEADROOM 32
|
2010-05-22 10:46:20 +00:00
|
|
|
|
2011-05-15 12:23:05 +00:00
|
|
|
static int layer2_read(struct osmo_fd *fd)
|
2010-05-22 10:46:20 +00:00
|
|
|
{
|
|
|
|
struct msgb *msg;
|
2011-11-06 20:55:40 +00:00
|
|
|
uint16_t len;
|
2010-05-22 10:46:20 +00:00
|
|
|
int rc;
|
|
|
|
|
2010-07-25 08:16:46 +00:00
|
|
|
msg = msgb_alloc_headroom(GSM_L2_LENGTH+GSM_L2_HEADROOM, GSM_L2_HEADROOM, "Layer2");
|
2010-05-22 10:46:20 +00:00
|
|
|
if (!msg) {
|
|
|
|
LOGP(DL1C, LOGL_ERROR, "Failed to allocate msg.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = read(fd->fd, &len, sizeof(len));
|
|
|
|
if (rc < sizeof(len)) {
|
2010-05-24 00:21:24 +00:00
|
|
|
fprintf(stderr, "Layer2 socket failed\n");
|
2010-11-14 11:50:14 +00:00
|
|
|
msgb_free(msg);
|
2010-05-24 14:46:11 +00:00
|
|
|
if (rc >= 0)
|
|
|
|
rc = -EIO;
|
2010-11-14 11:50:14 +00:00
|
|
|
layer2_close((struct osmocom_ms *) fd->data);
|
2020-02-25 23:13:04 +00:00
|
|
|
exit(102);
|
2010-05-22 10:46:20 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ntohs(len);
|
|
|
|
if (len > GSM_L2_LENGTH) {
|
|
|
|
LOGP(DL1C, LOGL_ERROR, "Length is too big: %u\n", len);
|
|
|
|
msgb_free(msg);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
msg->l1h = msgb_put(msg, len);
|
|
|
|
rc = read(fd->fd, msg->l1h, msgb_l1len(msg));
|
|
|
|
if (rc != msgb_l1len(msg)) {
|
|
|
|
LOGP(DL1C, LOGL_ERROR, "Can not read data: len=%d rc=%d "
|
|
|
|
"errno=%d\n", len, rc, errno);
|
|
|
|
msgb_free(msg);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
l1ctl_recv((struct osmocom_ms *) fd->data, msg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-15 12:23:05 +00:00
|
|
|
static int layer2_write(struct osmo_fd *fd, struct msgb *msg)
|
2010-05-22 10:46:20 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2010-11-14 11:50:14 +00:00
|
|
|
if (fd->fd <= 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-05-22 10:46:20 +00:00
|
|
|
rc = write(fd->fd, msg->data, msg->len);
|
|
|
|
if (rc != msg->len) {
|
|
|
|
LOGP(DL1C, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int layer2_open(struct osmocom_ms *ms, const char *socket_path)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2018-09-06 13:09:28 +00:00
|
|
|
rc = osmo_sock_unix_init_ofd(&ms->l2_wq.bfd, SOCK_STREAM, 0, socket_path, OSMO_SOCK_F_CONNECT);
|
2010-05-22 10:46:20 +00:00
|
|
|
if (rc < 0) {
|
2018-11-16 19:13:10 +00:00
|
|
|
LOGP(DL1C, LOGL_ERROR, "Failed to create unix domain socket %s: %s\n",
|
|
|
|
socket_path, strerror(-rc));
|
2010-05-22 10:46:20 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-05-15 12:23:14 +00:00
|
|
|
osmo_wqueue_init(&ms->l2_wq, 100);
|
2010-09-11 10:42:29 +00:00
|
|
|
ms->l2_wq.bfd.data = ms;
|
|
|
|
ms->l2_wq.read_cb = layer2_read;
|
|
|
|
ms->l2_wq.write_cb = layer2_write;
|
2010-05-22 10:46:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int layer2_close(struct osmocom_ms *ms)
|
|
|
|
{
|
2010-11-14 11:50:14 +00:00
|
|
|
if (ms->l2_wq.bfd.fd <= 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-09-11 10:42:29 +00:00
|
|
|
close(ms->l2_wq.bfd.fd);
|
2010-11-14 11:50:14 +00:00
|
|
|
ms->l2_wq.bfd.fd = -1;
|
2011-05-15 12:23:05 +00:00
|
|
|
osmo_fd_unregister(&ms->l2_wq.bfd);
|
2011-11-05 09:04:45 +00:00
|
|
|
osmo_wqueue_clear(&ms->l2_wq);
|
2010-05-22 10:46:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
|
|
|
|
{
|
2011-05-15 12:23:17 +00:00
|
|
|
DEBUGP(DL1C, "Sending: '%s'\n", osmo_hexdump(msg->data, msg->len));
|
2010-05-22 10:46:20 +00:00
|
|
|
|
|
|
|
if (msg->l1h != msg->data)
|
|
|
|
LOGP(DL1C, LOGL_ERROR, "Message L1 header != Message Data\n");
|
2018-08-11 10:55:43 +00:00
|
|
|
|
2010-05-22 10:46:20 +00:00
|
|
|
/* prepend 16bit length before sending */
|
2018-08-11 10:55:43 +00:00
|
|
|
msgb_push_u16(msg, msg->len);
|
2010-05-22 10:46:20 +00:00
|
|
|
|
2011-05-15 12:23:14 +00:00
|
|
|
if (osmo_wqueue_enqueue(&ms->l2_wq, msg) != 0) {
|
2010-05-22 10:46:20 +00:00
|
|
|
LOGP(DL1C, LOGL_ERROR, "Failed to enqueue msg.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|