patches and low-level development discussion
 help / color / mirror / code / Atom feed
a1ee8eee2f6325a91f9fc7b082d6feb3e10f1be5 blob 7897 bytes (raw)

  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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
 
/* Copyright 2020 Alyssa Ross
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#define _POSIX_C_SOURCE 200809L

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/virtwl.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

#include "virtio_wl.h"

// This is essentially vendored reusable library code, so I consider
// it exempt from the Wayland style guide. :)

#if defined(__GNUC__) && __GNUC__ >= 4
#define VIRTIO_WL_EXPORT __attribute__ ((visibility("default")))
#else
#define VIRTIO_WL_EXPORT
#endif

static int
set_nonblocking(int fd)
{
	int fl = fcntl(fd, F_GETFL);
	if (fl == -1)
		return -1;
	if (!(fl & O_NONBLOCK))
		if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) == -1)
			return -1;
	return fl;
}

// Returns the total size of all buffers in an iovec.
// A return value of -1 means that the total overflowed.
static ssize_t
iov_len(const struct iovec iov[], size_t n)
{
	size_t len = 0;
	for (size_t i = 0; i < n; i++) {
		if (SSIZE_MAX - len < iov[i].iov_len)
			return -1;
		len += iov[i].iov_len;
	}
	return len;
}

// Copies from an iovec array into a buffer.
// The buffer is assumed to be large enough to hold all the data.
// This length can be calculated with the iov_len function.
static size_t
iov_flatten(void *buf, size_t buflen, const struct iovec *iov, size_t iovlen)
{
	size_t off = 0;

	for (size_t index = 0; index < iovlen && off < buflen; index++) {
		const struct iovec *i = &iov[index];
		size_t rem = buflen - off;
		size_t len = i->iov_len < rem ? i->iov_len : rem;

		memcpy((unsigned char *)buf + off, i->iov_base, len);
		off += len;
	}

	return off;
}

// Copies from a buffer into an iovec array.
// Returns number of bytes copied.
static size_t
iov_fill(struct iovec *iov, size_t iovlen, const void *buf, size_t buflen)
{
	size_t off = 0;

	for (size_t index = 0; index < iovlen && off < buflen; index++) {
		struct iovec *i = &iov[index];
		size_t rem = buflen - off;
		size_t len = i->iov_len < rem ? i->iov_len : rem;

		memcpy(i->iov_base, (const unsigned char *)buf + off, len);
		off += len;
	}

	return off;
}

static ssize_t
cmsg_to_fdbuf(int *buf, size_t buflen, const struct msghdr *msg)
{
	size_t next_fd = 0;

	for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
	     cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) {
		size_t size = cmsg->cmsg_len - CMSG_LEN(0);

		// Check the cmsg can be handled.
		if (cmsg->cmsg_level != SOL_SOCKET ||
		    cmsg->cmsg_type != SCM_RIGHTS ||
		    size % sizeof(int) != 0) {
			errno = EINVAL;
			return -1;
		}

		size_t rem = sizeof(int) * (buflen - next_fd);
		size_t len = size > rem ? rem : size;

		// Copy the fds to the buffer.
		memcpy(buf + next_fd, CMSG_DATA(cmsg), len);
		next_fd += len / sizeof(int);

		if (size > rem)
			break;
	}

	return next_fd > SSIZE_MAX ? SSIZE_MAX : next_fd;
}

static size_t
fdbuf_to_cmsg(struct msghdr *msg, const int *buf, size_t buflen)
{
	// Check msg->msg_control is long enough to fit at least one fd.
	if (msg->msg_controllen < CMSG_SPACE(sizeof(int))) {

		// If there's at least one fd in buf, set MSG_CTRUNC.
		size_t i = 0;
		while (i < buflen && !(msg->msg_flags & MSG_CTRUNC))
			if (buf[i++] != -1)
				msg->msg_flags |= MSG_CTRUNC;

		return 0;
	}

	// cmsg(3):
	// > When initializing a buffer that will contain a series of cmsghdr
        // > structures (e.g., to be sent with sendmsg(2)), that buffer should
        // > first be zero-initialized to en‐ sure the correct operation of
        // > CMSG_NXTHDR().
	memset(msg->msg_control, 0, msg->msg_controllen);

	// Set up the cmsg.
	struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_RIGHTS;

	// Copy as many fds as fit into cmsg.
	size_t len = 0;
	for (size_t i = 0; i < buflen; i++) {
		if (buf[i] == -1)
			continue;

		if (CMSG_LEN((len + 1) * sizeof(int)) > msg->msg_controllen) {
			msg->msg_flags |= MSG_CTRUNC;
			break;
		}

		memcpy(CMSG_DATA(cmsg) + sizeof(int) * len, &buf[i], sizeof(int));
		len++;
	}

	cmsg->cmsg_len = CMSG_LEN(len * sizeof(int));
	return len;
}

VIRTIO_WL_EXPORT int
virtio_wl_connect(const char *name, uint32_t flags)
{
	static int wl_fd = -1;
	if (wl_fd < 0)
		wl_fd = open("/dev/wl0", O_RDWR | O_CLOEXEC);
	if (wl_fd < 0)
		return wl_fd;

	struct virtwl_ioctl_new new_ctx = {
		.type = name ? VIRTWL_IOCTL_NEW_CTX_NAMED : VIRTWL_IOCTL_NEW_CTX,
		.fd = -1,
		.flags = flags,
	};
	// Device assumes name 32 bytes long if not null terminated.
	if (name)
		strncpy(new_ctx.name, name, sizeof(new_ctx.name));

	if (ioctl(wl_fd, VIRTWL_IOCTL_NEW, &new_ctx))
		return -1;

	return new_ctx.fd;
}

VIRTIO_WL_EXPORT int
virtio_wl_send_raw(int sockfd, struct virtwl_ioctl_txn *ioctl_txn)
{
	return ioctl(sockfd, VIRTWL_IOCTL_SEND, ioctl_txn);
}

static int
msghdr_to_txn(const struct msghdr *msg, struct virtwl_ioctl_txn **txn)
{
	// Make sure that if there's an error and the caller tries to free *txn
	// anyway it doesn't end up freeing an invalid address.
	*txn = NULL;

	// Figure out how big the txn needs to be.
	ssize_t len = iov_len(msg->msg_iov, msg->msg_iovlen);
	if (len < 0 || len > UINT32_MAX) {
		errno = ENOMEM;
		return -1;
	}

	// Allocate the txn.
	*txn = malloc(sizeof(**txn) + len);
	if (!*txn)
		return -1;

	// Set the len member of the txn.
	(*txn)->len = len;

	// Copy data from the iovec into the transaction.
	iov_flatten((*txn)->data, len, msg->msg_iov, msg->msg_iovlen);

	// Copy file descriptors to the txn.
	ssize_t fd_count = cmsg_to_fdbuf((*txn)->fds, VIRTWL_SEND_MAX_ALLOCS, msg);
	if (fd_count == -1) {
		free(*txn);
		*txn = NULL;
		return -1;
	}

	// Fill the rest of the fd buffer with -1.
	while (fd_count < VIRTWL_SEND_MAX_ALLOCS)
		(*txn)->fds[fd_count++] = -1;

	return 0;
}

static void
txn_to_msghdr(struct msghdr *msg, const struct virtwl_ioctl_txn *txn)
{
	// Copy txn data to iovecs.
	iov_fill(msg->msg_iov, msg->msg_iovlen, txn->data, txn->len);

	// Copy fds to cmsg.
	fdbuf_to_cmsg(msg, txn->fds, VIRTWL_SEND_MAX_ALLOCS);
}

VIRTIO_WL_EXPORT ssize_t
virtio_wl_sendmsg(int sockfd, const struct msghdr *msg, int flags)
{
	int sockfl;

	if ((flags & ~MSG_DONTWAIT) != MSG_NOSIGNAL) {
		errno = EINVAL;
		return -1;
	}

	struct virtwl_ioctl_txn *txn;
	if (msghdr_to_txn(msg, &txn) == -1)
		return -1;

	if (flags & MSG_DONTWAIT) {
		sockfl = set_nonblocking(sockfd);
		if (sockfl == -1) {
			free(txn);
			return -1;
		}
	}

	int rv = virtio_wl_send_raw(sockfd, txn);

	if ((flags & MSG_DONTWAIT) && !(sockfl & O_NONBLOCK)) {
		if (fcntl(sockfd, F_SETFL, sockfl) == -1) {
			free(txn);
			return -1;
		}
	}

	if (rv != -1)
		rv = txn->len;

	free(txn);
	return rv;
}

VIRTIO_WL_EXPORT int
virtio_wl_recv_raw(int sockfd, struct virtwl_ioctl_txn *ioctl_txn)
{
	return ioctl(sockfd, VIRTWL_IOCTL_RECV, ioctl_txn);
}

VIRTIO_WL_EXPORT ssize_t
virtio_wl_recvmsg(int sockfd, struct msghdr *msg, int flags)
{
	int sockfl;

	if ((flags & ~MSG_DONTWAIT) != MSG_CMSG_CLOEXEC) {
		errno = EINVAL;
		return -1;
	}

	ssize_t len = iov_len(msg->msg_iov, msg->msg_iovlen);
	if (len < 0 || len > UINT32_MAX) {
		errno = ENOMEM;
		return -1;
	}

	struct virtwl_ioctl_txn *txn = malloc(sizeof(*txn) + len);
	if (!txn)
		return -1;

	txn->len = len;

	if (flags & MSG_DONTWAIT) {
		sockfl = set_nonblocking(sockfd);
		if (sockfl == -1) {
			free(txn);
			return -1;
		}
	}

	ssize_t rv = virtio_wl_recv_raw(sockfd, txn);

	if ((flags & MSG_DONTWAIT) && !(sockfl & O_NONBLOCK))
		if (fcntl(sockfd, F_SETFL, sockfl) == -1)
			rv = -1;

	if (rv == -1) {
		for (size_t i = 0; i < VIRTWL_SEND_MAX_ALLOCS; i++)
			if (txn->fds[i] == -1)
				close(txn->fds[i]);
	} else {
		txn_to_msghdr(msg, txn);
		rv = txn->len;
	}

	free(txn);
	return rv;
}
debug log:

solving a1ee8ee ...
found a1ee8ee in https://spectrum-os.org/lists/archives/spectrum-devel/20200708151600.10607-1-hi@alyssa.is/

applying [1/1] https://spectrum-os.org/lists/archives/spectrum-devel/20200708151600.10607-1-hi@alyssa.is/
diff --git a/src/virtio_wl.c b/src/virtio_wl.c
new file mode 100644
index 0000000..a1ee8ee

Checking patch src/virtio_wl.c...
Applied patch src/virtio_wl.c cleanly.

index at:
100644 a1ee8eee2f6325a91f9fc7b082d6feb3e10f1be5	src/virtio_wl.c

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).