summaryrefslogtreecommitdiffstats
path: root/src/host/trxcon/l1ctl_link.c
blob: c38a87155999b0cb7565543412b0404645e1e118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * OsmocomBB <-> SDR connection bridge
 * GSM L1 control socket (/tmp/osmocom_l2) handlers
 *
 * (C) 2013 by Sylvain Munaut <tnt@246tNt.com>
 * (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <sys/un.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include <osmocom/core/fsm.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/select.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/write_queue.h>

#include "trxcon.h"
#include "logging.h"
#include "l1ctl_link.h"
#include "l1ctl.h"

static struct osmo_fsm_state l1ctl_fsm_states[] = {
	[L1CTL_STATE_IDLE] = {
		.out_state_mask = GEN_MASK(L1CTL_STATE_CONNECTED),
		.name = "IDLE",
	},
	[L1CTL_STATE_CONNECTED] = {
		.out_state_mask = GEN_MASK(L1CTL_STATE_IDLE),
		.name = "CONNECTED",
	},
};

static struct osmo_fsm l1ctl_fsm = {
	.name = "l1ctl_link_fsm",
	.states = l1ctl_fsm_states,
	.num_states = ARRAY_SIZE(l1ctl_fsm_states),
	.log_subsys = DL1C,
};

static int l1ctl_link_read_cb(struct osmo_fd *bfd)
{
	struct l1ctl_link *l1l = (struct l1ctl_link *) bfd->data;
	struct msgb *msg;
	uint16_t len;
	int rc;

	/* Allocate a new msg */
	msg = msgb_alloc_headroom(L1CTL_LENGTH + L1CTL_HEADROOM,
		L1CTL_HEADROOM, "L1CTL");
	if (!msg) {
		fprintf(stderr, "Failed to allocate msg\n");
		return -ENOMEM;
	}

	/* Attempt to read from socket */
	rc = read(bfd->fd, &len, sizeof(len));
	if (rc < sizeof(len)) {
		LOGP(DL1D, LOGL_NOTICE, "L1CTL has lost connection\n");
		msgb_free(msg);
		if (rc >= 0)
			rc = -EIO;
		l1ctl_link_close_conn(l1l);
		return rc;
	}

	/* Check message length */
	len = ntohs(len);
	if (len > L1CTL_LENGTH) {
		LOGP(DL1D, LOGL_ERROR, "Length is too big: %u\n", len);
		msgb_free(msg);
		return -EINVAL;
	}

	msg->l1h = msgb_put(msg, len);
	rc = read(bfd->fd, msg->l1h, msgb_l1len(msg));
	if (rc != len) {
		LOGP(DL1D, LOGL_ERROR, "Can not read data: len=%d < rc=%d: "
			"%s\n", len, rc, strerror(errno));
		msgb_free(msg);
		return rc;
	}

	/* Debug print */
	LOGP(DL1D, LOGL_DEBUG, "RX: '%s'\n",
		osmo_hexdump(msg->data, msg->len));

	/* Call L1CTL handler */
	l1ctl_rx_cb(l1l, msg);

	return 0;
}

static int l1ctl_link_write_cb(struct osmo_fd *bfd, struct msgb *msg)
{
	int len;

	if (bfd->fd <= 0)
		return -EINVAL;

	len = write(bfd->fd, msg->data, msg->len);
	if (len != msg->len) {
		LOGP(DL1D, LOGL_ERROR, "Failed to write data: "
			"written (%d) < msg_len (%d)\n", len, msg->len);
		return -1;
	}

	return 0;
}

/* Connection handler */
static int l1ctl_link_accept(struct osmo_fd *bfd, unsigned int flags)
{
	struct l1ctl_link *l1l = (struct l1ctl_link *) bfd->data;
	struct osmo_fd *conn_bfd = &l1l->wq.bfd;
	struct sockaddr_un un_addr;
	socklen_t len;
	int cfd;

	len = sizeof(un_addr);
	cfd = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
	if (cfd < 0) {
		LOGP(DL1C, LOGL_ERROR, "Failed to accept a new connection\n");
		return -1;
	}

	/* Check if we already have an active connection */
	if (conn_bfd->fd != -1) {
		LOGP(DL1C, LOGL_NOTICE, "A new connection rejected: "
			"we already have another active\n");
		close(cfd);
		return 0;
	}

	osmo_wqueue_init(&l1l->wq, 100);
	INIT_LLIST_HEAD(&conn_bfd->list);

	l1l->wq.write_cb = l1ctl_link_write_cb;
	l1l->wq.read_cb = l1ctl_link_read_cb;
	conn_bfd->when = BSC_FD_READ;
	conn_bfd->data = l1l;
	conn_bfd->fd = cfd;

	if (osmo_fd_register(conn_bfd) != 0) {
		LOGP(DL1C, LOGL_ERROR, "Failed to register new connection fd\n");
		close(conn_bfd->fd);
		conn_bfd->fd = -1;
		return -1;
	}

	osmo_fsm_inst_dispatch(trxcon_fsm, L1CTL_EVENT_CONNECT, l1l);
	osmo_fsm_inst_state_chg(l1l->fsm, L1CTL_STATE_CONNECTED, 0, 0);

	LOGP(DL1C, LOGL_NOTICE, "L1CTL has a new connection\n");

	return 0;
}

int l1ctl_link_send(struct l1ctl_link *l1l, struct msgb *msg)
{
	uint16_t *len;

	/* Debug print */
	LOGP(DL1D, LOGL_DEBUG, "TX: '%s'\n",
		osmo_hexdump(msg->data, msg->len));

	if (msg->l1h != msg->data)
		LOGP(DL1D, LOGL_INFO, "Message L1 header != Message Data\n");

	/* Prepend 16-bit length before sending */
	len = (uint16_t *) msgb_push(msg, sizeof(*len));
	*len = htons(msg->len - sizeof(*len));

	if (osmo_wqueue_enqueue(&l1l->wq, msg) != 0) {
		LOGP(DL1D, LOGL_ERROR, "Failed to enqueue msg!\n");
		msgb_free(msg);
		return -EIO;
	}

	return 0;
}

int l1ctl_link_close_conn(struct l1ctl_link *l1l)
{
	struct osmo_fd *conn_bfd = &l1l->wq.bfd;

	if (conn_bfd->fd <= 0)
		return -EINVAL;

	/* Close connection socket */
	osmo_fd_unregister(conn_bfd);
	close(conn_bfd->fd);
	conn_bfd->fd = -1;

	/* Clear pending messages */
	osmo_wqueue_clear(&l1l->wq);

	osmo_fsm_inst_dispatch(trxcon_fsm, L1CTL_EVENT_DISCONNECT, l1l);
	osmo_fsm_inst_state_chg(l1l->fsm, L1CTL_STATE_IDLE, 0, 0);

	return 0;
}

int l1ctl_link_init(struct l1ctl_link **l1l, const char *sock_path)
{
	struct l1ctl_link *l1l_new;
	struct osmo_fd *bfd;
	int rc;

	LOGP(DL1C, LOGL_NOTICE, "Init L1CTL link (%s)\n", sock_path);

	l1l_new = talloc_zero(tall_trx_ctx, struct l1ctl_link);
	if (!l1l_new) {
		fprintf(stderr, "Failed to allocate memory\n");
		return -ENOMEM;
	}

	/* Create a socket and bind handlers */
	bfd = &l1l_new->listen_bfd;
	rc = osmo_sock_unix_init_ofd(bfd, SOCK_STREAM, 0, sock_path,
		OSMO_SOCK_F_BIND);
	if (rc < 0) {
		fprintf(stderr, "Could not create UNIX socket: %s\n",
			strerror(errno));
		talloc_free(l1l_new);
		return rc;
	}

	/* Bind shutdown handler */
	l1l_new->shutdown_cb = l1ctl_shutdown_cb;

	/* Bind connection handler */
	bfd->cb = l1ctl_link_accept;
	bfd->when = BSC_FD_READ;
	bfd->data = l1l_new;

	/**
	 * To be able to accept first connection and
	 * drop others, it should be set to -1
	 */
	l1l_new->wq.bfd.fd = -1;

	/* Allocate a new dedicated state machine */
	osmo_fsm_register(&l1ctl_fsm);
	l1l_new->fsm = osmo_fsm_inst_alloc(&l1ctl_fsm, l1l_new,
		NULL, LOGL_DEBUG, sock_path);

	*l1l = l1l_new;

	return 0;
}

void l1ctl_link_shutdown(struct l1ctl_link *l1l)
{
	struct osmo_fd *listen_bfd;

	/* May be unallocated due to init error */
	if (!l1l)
		return;

	LOGP(DL1C, LOGL_NOTICE, "Shutdown L1CTL link\n");

	/* Call shutdown callback */
	if (l1l->shutdown_cb != NULL)
		l1l->shutdown_cb(l1l);

	listen_bfd = &l1l->listen_bfd;

	/* Check if we have an established connection */
	if (l1l->wq.bfd.fd != -1)
		l1ctl_link_close_conn(l1l);

	/* Unbind listening socket */
	if (listen_bfd->fd != -1) {
		osmo_fd_unregister(listen_bfd);
		close(listen_bfd->fd);
		listen_bfd->fd = -1;
	}

	osmo_fsm_inst_free(l1l->fsm);
	talloc_free(l1l);
}