-
Notifications
You must be signed in to change notification settings - Fork 34
/
sockets.c
530 lines (428 loc) · 12 KB
/
sockets.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Authors: Alexander Jung <[email protected]>
* Marc Rittinghaus <[email protected]>
*
* Copyright (c) 2020, NEC Laboratories Europe GmbH, NEC Corporation.
* All rights reserved.
* Copyright (c) 2021, Karlsruhe Institute of Technology (KIT).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <uk/config.h>
#include <uk/assert.h>
#include <uk/print.h>
#include <uk/essentials.h>
#include <uk/socket_driver.h>
#include <lwip/sockets.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <lwip/priv/sockets_priv.h>
#include <lwip/api.h>
#include <lwip/sys.h>
#include <lwip/tcp.h>
#include <lwip/tcpbase.h>
static inline
int _lwip_getfd(posix_sock *sock)
{
return (intptr_t)posix_sock_get_data(sock);
}
static int
lwip_socket_apply_flags(int lwip_fd, int flags)
{
int val;
if (flags & SOCK_NONBLOCK) {
val = 1;
val = lwip_ioctl(lwip_fd, FIONBIO, &val);
if (unlikely(val < 0)) {
return -errno;
}
}
/* Ignore SOCK_CLOEXEC */
return 0;
}
static void *
lwip_posix_socket_create(struct posix_socket_driver *d, int family, int type,
int protocol)
{
int lwip_fd;
int flags, rc;
/* Blocking is handled by posix-socket */
flags = (type & SOCK_FLAGS) | SOCK_NONBLOCK;
type = type & ~SOCK_FLAGS;
lwip_fd = lwip_socket(family, type, protocol);
if (unlikely(lwip_fd < 0))
return ERR2PTR(-errno);
rc = lwip_socket_apply_flags(lwip_fd, flags);
if (unlikely(rc)) {
(void)lwip_close(lwip_fd);
return ERR2PTR(rc);
}
return (void *)(intptr_t)lwip_fd;
}
static void *
lwip_posix_socket_accept4(posix_sock *file,
struct sockaddr *restrict addr,
socklen_t *restrict addr_len, int flags)
{
int listen_fd, new_fd;
int rc;
listen_fd = _lwip_getfd(file);
UK_ASSERT(listen_fd >= 0);
new_fd = lwip_accept(listen_fd, addr, addr_len);
if (unlikely(new_fd < 0))
return ERR2PTR(-errno);
flags |= SOCK_NONBLOCK; /* Blocking is handled by posix-socket */
rc = lwip_socket_apply_flags(new_fd, flags);
if (unlikely(rc)) {
(void)lwip_close(new_fd);
return ERR2PTR(rc);
}
return (void *)(intptr_t)new_fd;
}
static int
lwip_posix_socket_bind(posix_sock *file,
const struct sockaddr *addr,
socklen_t addr_len)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_bind(lwip_fd, addr, addr_len);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_shutdown(posix_sock *file, int how)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_shutdown(lwip_fd, how);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_getpeername(posix_sock *file,
struct sockaddr *restrict addr,
socklen_t *restrict addr_len)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_getpeername(lwip_fd, addr, addr_len);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_getsockname(posix_sock *file,
struct sockaddr *restrict addr,
socklen_t *restrict addr_len)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_getsockname(lwip_fd, addr, addr_len);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_getsockopt(posix_sock *file, int level,
int optname, void *restrict optval,
socklen_t *restrict optlen)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_getsockopt(lwip_fd, level, optname, optval, optlen);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
#define LINUX_SOL_TCP 6
#define LINUX_TCP_FASTOPEN 23
static int
lwip_posix_socket_setsockopt(posix_sock *file, int level,
int optname, const void *optval, socklen_t optlen)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
if ((level == LINUX_SOL_TCP && optname == LINUX_TCP_FASTOPEN) ||
(level == SOL_IP && optname == IP_RECVERR)) {
/* Ignore stuff that LWIP doesn't support */
return 0;
}
ret = lwip_setsockopt(lwip_fd, level, optname, optval, optlen);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_connect(posix_sock *file,
const struct sockaddr *addr, socklen_t addr_len)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_connect(lwip_fd, addr, addr_len);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_listen(posix_sock *file, int backlog)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_listen(lwip_fd, backlog);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static ssize_t
lwip_posix_socket_recvfrom(posix_sock *file, void *restrict buf,
size_t len, int flags, struct sockaddr *from,
socklen_t *restrict fromlen)
{
int lwip_fd;
ssize_t ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_recvfrom(lwip_fd, buf, len, flags, from, fromlen);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static ssize_t
lwip_posix_socket_recvmsg(posix_sock *file, struct msghdr *msg,
int flags)
{
int lwip_fd;
ssize_t ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_recvmsg(lwip_fd, msg, flags);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static ssize_t
lwip_posix_socket_sendmsg(posix_sock *file,
const struct msghdr *msg, int flags)
{
int lwip_fd;
ssize_t ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_sendmsg(lwip_fd, msg, flags);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static ssize_t
lwip_posix_socket_sendto(posix_sock *file, const void *buf,
size_t len, int flags,
const struct sockaddr *dest_addr,
socklen_t addrlen)
{
int lwip_fd;
ssize_t ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_sendto(lwip_fd, buf, len, flags,
dest_addr, addrlen);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static ssize_t
lwip_posix_socket_read(posix_sock *file, const struct iovec *iov,
int iovcnt)
{
int lwip_fd;
ssize_t ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_readv(lwip_fd, iov, iovcnt);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static ssize_t
lwip_posix_socket_write(posix_sock *file, const struct iovec *iov,
int iovcnt)
{
int lwip_fd;
ssize_t ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_writev(lwip_fd, iov, iovcnt);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_close(posix_sock *file)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_close(lwip_fd);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
static int
lwip_posix_socket_ioctl(posix_sock *file, int request, void *argp)
{
int lwip_fd;
int ret;
lwip_fd = _lwip_getfd(file);
UK_ASSERT(lwip_fd >= 0);
ret = lwip_ioctl(lwip_fd, request, argp);
if (unlikely(ret < 0))
ret = -errno;
return ret;
}
#if LWIP_NETCONN_FULLDUPLEX
#define NETCONN_RECVMBOX_WAITABLE(conn) \
(sys_mbox_valid(&(conn)->recvmbox) && \
(((conn)->flags & NETCONN_FLAG_MBOXINVALID) == 0))
#else /* LWIP_NETCONN_FULLDUPLEX */
#define NETCONN_RECVMBOX_WAITABLE(conn) \
sys_mbox_valid(&(conn)->recvmbox)
#endif /* LWIP_NETCONN_FULLDUPLEX */
static unsigned int
get_lwip_socket_events(struct lwip_sock *sock)
{
unsigned int events = 0;
UK_ASSERT(sock);
if ((NETCONNTYPE_GROUP(sock->conn->type) == NETCONN_TCP)) {
/* A TCP connection may be in not-connected state. Don't report it as
* readable or writeable.
*/
if ((sock->conn->state == NETCONN_NONE) &&
(!NETCONN_RECVMBOX_WAITABLE(sock->conn))) {
if (sock->errevent != 0)
events |= EPOLLERR;
return events;
}
/* Check whether the TCP connection is in CLOSE_WAIT (= got a
* FIN packet). In that case the peer will not sent more data
* and we can tell the application that the receive buffer is
* everything we got until the EOF.
*/
if (sock->conn->pcb.tcp == NULL ||
sock->conn->pcb.tcp->state == CLOSE_WAIT)
events |= EPOLLRDHUP;
}
if (sock->lastdata.pbuf || sock->rcvevent > 0)
events |= EPOLLIN | EPOLLRDNORM;
if (sock->sendevent != 0)
events |= EPOLLOUT | EPOLLWRNORM;
if (sock->errevent != 0)
events |= EPOLLERR;
return events;
}
void
lwip_posix_socket_event_callback(struct lwip_sock *sock,
enum netconn_evt evt __unused,
u16_t len __unused)
{
posix_sock *sockobj;
unsigned int events;
UK_ASSERT(sock);
if (unlikely(!sock->sock_data))
return;
sockobj = (posix_sock *)sock->sock_data;
UK_ASSERT(_lwip_getfd(sockobj) == sock->conn->socket);
events = get_lwip_socket_events(sock);
posix_sock_event_assign(sockobj, events);
}
static void
lwip_posix_socket_poll(posix_sock *file)
{
int lwip_fd;
unsigned revents;
struct lwip_sock *sock;
SYS_ARCH_DECL_PROTECT(lev);
lwip_fd = _lwip_getfd(file);
SYS_ARCH_PROTECT(lev);
/* This is a bit hacky but lwip does not provide a different public
* interface to get a reference to the socket. Furthermore, this
* function does not increase the reference count which is good
* as we do not hold the reference longer than the lock anyways. Since
* we need to hold the lock for evaluating the socket state this fits
* in well.
*/
sock = lwip_socket_dbg_get_socket(lwip_fd);
revents = get_lwip_socket_events(sock);
sock->sock_data = (void *)file;
SYS_ARCH_UNPROTECT(lev);
posix_sock_event_assign(file, revents);
}
static struct posix_socket_ops lwip_posix_socket_ops = {
/* POSIX interfaces */
.create = lwip_posix_socket_create,
.accept4 = lwip_posix_socket_accept4,
.bind = lwip_posix_socket_bind,
.shutdown = lwip_posix_socket_shutdown,
.getpeername = lwip_posix_socket_getpeername,
.getsockname = lwip_posix_socket_getsockname,
.getsockopt = lwip_posix_socket_getsockopt,
.setsockopt = lwip_posix_socket_setsockopt,
.connect = lwip_posix_socket_connect,
.listen = lwip_posix_socket_listen,
.recvfrom = lwip_posix_socket_recvfrom,
.recvmsg = lwip_posix_socket_recvmsg,
.sendmsg = lwip_posix_socket_sendmsg,
.sendto = lwip_posix_socket_sendto,
/* vfscore ops */
.read = lwip_posix_socket_read,
.write = lwip_posix_socket_write,
.close = lwip_posix_socket_close,
.ioctl = lwip_posix_socket_ioctl,
.poll = lwip_posix_socket_poll,
};
POSIX_SOCKET_FAMILY_REGISTER(AF_INET, &lwip_posix_socket_ops);
#ifdef CONFIG_LWIP_IPV6
POSIX_SOCKET_FAMILY_REGISTER(AF_INET6, &lwip_posix_socket_ops);
#endif /* CONFIG_LWIP_IPV6 */