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
531
532
533
534
535
536
537
538
539
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
*/
/*
* Test ancillary data receipt via recvmsg()
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <pthread.h>
#include <err.h>
static boolean_t debug;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t cmutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t ccv = PTHREAD_COND_INITIALIZER;
static boolean_t server_ready = _B_FALSE;
static boolean_t client_done = _B_FALSE;
static in_addr_t testip;
#define DEBUG(x) if (debug) printf x
#define TESTPORT 32123
#define RT_RECVTOS 0x1
#define RT_RECVTTL 0x2
#define RT_RECVPKTINFO 0x4
#define RT_RECVMASK 0x7
#define RT_SETTOS 0x10
#define RT_SETTTL 0x20
#define RT_STREAM 0x40
#define RT_SKIP 0x80
typedef struct recvmsg_test {
char *name; /* Name of the test */
uint8_t tos; /* TOS to set */
uint8_t ttl; /* TTL to set */
uint8_t flags; /* Test flags, RT_ */
} recvmsg_test_t;
static recvmsg_test_t tests[] = {
{
.name = "baseline",
.flags = 0,
},
/* Combinations of receive flags */
{
.name = "recv TOS",
.flags = RT_RECVTOS,
},
{
.name = "recv TTL",
.flags = RT_RECVTTL,
},
{
.name = "recv PKTINFO",
.flags = RT_RECVPKTINFO,
},
{
.name = "recv TOS,TTL",
.flags = RT_RECVTOS | RT_RECVTTL,
},
{
.name = "recv TTL,PKTINFO",
.flags = RT_RECVTTL | RT_RECVPKTINFO,
},
{
.name = "recv TOS,PKTINFO",
.flags = RT_RECVTOS | RT_RECVPKTINFO,
},
{
.name = "recv TOS,TTL,PKTINFO",
.flags = RT_RECVTOS | RT_RECVTTL | RT_RECVPKTINFO,
},
/* Manually set TTL and TOS */
{
.name = "set TOS,TTL",
.flags = RT_SETTOS | RT_SETTTL,
.ttl = 11,
.tos = 0xe0
},
{
.name = "set/recv TOS,TTL",
.flags = RT_SETTOS | RT_SETTTL | RT_RECVTOS | RT_RECVTTL,
.ttl = 32,
.tos = 0x48
},
{
.name = "set TOS,TTL, recv PKTINFO",
.flags = RT_SETTOS | RT_SETTTL | RT_RECVPKTINFO,
.ttl = 173,
.tos = 0x78
},
{
.name = "set TOS,TTL, recv TOS,TTL,PKTINFO",
.flags = RT_SETTOS | RT_SETTTL | RT_RECVTOS | RT_RECVTTL |
RT_RECVPKTINFO,
.ttl = 54,
.tos = 0x90
},
/* STREAM socket */
{
.name = "STREAM set TOS",
.flags = RT_STREAM | RT_SETTOS,
.tos = 0xe0
},
/*
* The ancillary data are not returned for the loopback TCP path,
* so these tests are skipped by default.
* To run them, use two different zones (or machines) and run:
* recvmsg.64 -s 'test name'
* on the first, and:
* recvmsg.64 -c <first machine IP> 'test name'
* on the second.
*/
{
.name = "STREAM recv TOS",
.flags = RT_STREAM | RT_RECVTOS | RT_SKIP,
},
{
.name = "STREAM set/recv TOS",
.flags = RT_STREAM | RT_SETTOS | RT_RECVTOS | RT_SKIP,
.tos = 0x48
},
/* End of tests */
{
.name = NULL
}
};
static boolean_t
servertest(recvmsg_test_t *t)
{
struct sockaddr_in addr;
boolean_t pass = _B_TRUE;
int sockfd, readfd, acceptfd = -1, c = 1;
DEBUG(("\nserver %s: starting\n", t->name));
sockfd = socket(AF_INET,
t->flags & RT_STREAM ? SOCK_STREAM : SOCK_DGRAM, 0);
if (sockfd == -1)
err(EXIT_FAILURE, "failed to create server socket");
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(TESTPORT);
if (bind(sockfd, (struct sockaddr *)&addr, sizeof (addr)) == -1)
err(EXIT_FAILURE, "server socket bind failed");
if (t->flags & RT_RECVTOS) {
DEBUG((" : setting RECVTOS\n"));
if (setsockopt(sockfd, IPPROTO_IP, IP_RECVTOS, &c,
sizeof (c)) == -1) {
printf("[FAIL] %s - "
"couldn't set TOS on server socket: %s\n",
t->name, strerror(errno));
pass = _B_FALSE;
}
}
if (t->flags & RT_RECVTTL) {
DEBUG((" : setting RECVTTL\n"));
if (setsockopt(sockfd, IPPROTO_IP, IP_RECVTTL, &c,
sizeof (c)) == -1) {
printf("[FAIL] %s - "
"couldn't set TTL on server socket: %s\n",
t->name, strerror(errno));
pass = _B_FALSE;
}
}
if (t->flags & RT_RECVPKTINFO) {
DEBUG((" : setting RECVPKTINFO\n"));
if (setsockopt(sockfd, IPPROTO_IP, IP_PKTINFO, &c,
sizeof (c)) == -1) {
printf("[FAIL] %s - "
"couldn't set PKTINFO on server socket: %s\n",
t->name, strerror(errno));
pass = _B_FALSE;
}
}
if (t->flags & RT_STREAM) {
if (listen(sockfd, 1) == -1)
err(EXIT_FAILURE, "Could not listen on sever socket");
}
/* Signal the client that the server is ready for the next test */
if (debug)
printf(" : signalling client\n");
(void) pthread_mutex_lock(&mutex);
server_ready = _B_TRUE;
(void) pthread_cond_signal(&cv);
(void) pthread_mutex_unlock(&mutex);
if (t->flags & RT_STREAM) {
struct sockaddr_in caddr;
socklen_t sl = sizeof (caddr);
if ((acceptfd = accept(sockfd, (struct sockaddr *)&caddr,
&sl)) == -1) {
err(EXIT_FAILURE, "socket accept failed");
}
readfd = acceptfd;
} else {
readfd = sockfd;
}
/* Receive the datagram */
struct msghdr msg;
char buf[0x100];
char cbuf[CMSG_SPACE(0x400)];
struct iovec iov[1] = {0};
ssize_t r;
iov[0].iov_base = buf;
iov[0].iov_len = sizeof (buf);
bzero(&msg, sizeof (msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof (cbuf);
DEBUG((" : waiting for message\n"));
r = recvmsg(readfd, &msg, 0);
if (r <= 0) {
printf("[FAIL] %s - recvmsg returned %d (%s)\n",
t->name, r, strerror(errno));
pass = _B_FALSE;
goto out;
}
DEBUG((" : recvmsg returned %d (flags=0x%x, controllen=%d)\n",
r, msg.msg_flags, msg.msg_controllen));
if (r != strlen(t->name)) {
printf("[FAIL] %s - got '%.*s' (%d bytes), expected '%s'\n",
t->name, r, buf, r, t->name);
pass = _B_FALSE;
}
DEBUG((" : Received '%.*s'\n", r, buf));
if (msg.msg_flags != 0) {
printf("[FAIL] %s - received flags 0x%x\n",
t->name, msg.msg_flags);
pass = _B_FALSE;
}
uint8_t flags = 0;
for (struct cmsghdr *cm = CMSG_FIRSTHDR(&msg); cm != NULL;
cm = CMSG_NXTHDR(&msg, cm)) {
uint8_t d;
DEBUG((" : >> Got cmsg %x/%x - length %u\n",
cm->cmsg_level, cm->cmsg_type, cm->cmsg_len));
if (cm->cmsg_level != IPPROTO_IP)
continue;
switch (cm->cmsg_type) {
case IP_PKTINFO:
flags |= RT_RECVPKTINFO;
if (debug) {
struct in_pktinfo *pi =
(struct in_pktinfo *)CMSG_DATA(cm);
printf(" : ifIndex: %u\n", pi->ipi_ifindex);
}
break;
case IP_RECVTTL:
if (cm->cmsg_len != CMSG_LEN(sizeof (uint8_t))) {
printf(
"[FAIL] %s - cmsg_len was %u expected %u\n",
t->name, cm->cmsg_len,
CMSG_LEN(sizeof (uint8_t)));
pass = _B_FALSE;
break;
}
flags |= RT_RECVTTL;
memcpy(&d, CMSG_DATA(cm), sizeof (d));
DEBUG((" : RECVTTL = %u\n", d));
if (t->flags & RT_SETTTL && d != t->ttl) {
printf("[FAIL] %s - TTL was %u, expected %u\n",
t->name, d, t->ttl);
pass = _B_FALSE;
}
break;
case IP_RECVTOS:
if (cm->cmsg_len != CMSG_LEN(sizeof (uint8_t))) {
printf(
"[FAIL] %s - cmsg_len was %u expected %u\n",
t->name, cm->cmsg_len,
CMSG_LEN(sizeof (uint8_t)));
pass = _B_FALSE;
break;
}
flags |= RT_RECVTOS;
memcpy(&d, CMSG_DATA(cm), sizeof (d));
DEBUG((" : RECVTOS = %u\n", d));
if (t->flags & RT_SETTOS && d != t->tos) {
printf("[FAIL] %s - TOS was %u, expected %u\n",
t->name, d, t->tos);
pass = _B_FALSE;
}
break;
}
}
if ((t->flags & RT_RECVMASK) != flags) {
printf("[FAIL] %s - Did not receive everything expected, "
"flags %#x vs. %#x\n", t->name,
flags, t->flags & RT_RECVMASK);
pass = _B_FALSE;
}
/* Wait for the client to finish */
(void) pthread_mutex_lock(&cmutex);
while (!client_done)
(void) pthread_cond_wait(&ccv, &cmutex);
client_done = _B_FALSE;
(void) pthread_mutex_unlock(&cmutex);
out:
if (acceptfd != -1)
(void) close(acceptfd);
(void) close(sockfd);
if (pass)
printf("[PASS] %s\n", t->name);
return (pass);
}
static int
server(const char *test)
{
int ret = EXIT_SUCCESS;
recvmsg_test_t *t;
for (t = tests; t->name != NULL; t++) {
if (test != NULL) {
if (strcmp(test, t->name) != 0)
continue;
client_done = _B_TRUE;
return (servertest(t));
}
if (t->flags & RT_SKIP) {
printf("[SKIP] %s - (requires two separate zones)\n",
t->name);
continue;
}
if (!servertest(t))
ret = EXIT_FAILURE;
}
return (ret);
}
static void
clienttest(recvmsg_test_t *t)
{
struct sockaddr_in addr;
int s, ret;
DEBUG(("client %s: starting\n", t->name));
s = socket(AF_INET, t->flags & RT_STREAM ? SOCK_STREAM : SOCK_DGRAM, 0);
if (s == -1)
err(EXIT_FAILURE, "failed to create client socket");
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = testip;
addr.sin_port = htons(TESTPORT);
if (t->flags & RT_STREAM) {
if (connect(s, (struct sockaddr *)&addr, sizeof (addr)) == -1)
err(EXIT_FAILURE, "failed to connect to server");
}
if (t->flags & RT_SETTOS) {
int c = t->tos;
DEBUG(("client %s: setting TOS = 0x%x\n", t->name, c));
if (setsockopt(s, IPPROTO_IP, IP_TOS, &c, sizeof (c)) == -1)
err(EXIT_FAILURE, "could not set TOS on client socket");
}
if (t->flags & RT_SETTTL) {
int c = t->ttl;
DEBUG(("client %s: setting TTL = 0x%x\n", t->name, c));
if (setsockopt(s, IPPROTO_IP, IP_TTL, &c, sizeof (c)) == -1)
err(EXIT_FAILURE, "could not set TTL on client socket");
}
DEBUG(("client %s: sending\n", t->name));
if (t->flags & RT_STREAM) {
ret = send(s, t->name, strlen(t->name), 0);
shutdown(s, SHUT_RDWR);
} else {
ret = sendto(s, t->name, strlen(t->name), 0,
(struct sockaddr *)&addr, sizeof (addr));
}
if (ret == -1)
err(EXIT_FAILURE, "sendto failed to send data to server");
DEBUG(("client %s: done\n", t->name));
close(s);
}
static void *
client(void *arg)
{
char *test = (char *)arg;
recvmsg_test_t *t;
for (t = tests; t->name != NULL; t++) {
if (test != NULL) {
if (strcmp(test, t->name) != 0)
continue;
clienttest(t);
return (NULL);
}
if (t->flags & RT_SKIP)
continue;
/* Wait for the server to be ready to receive */
(void) pthread_mutex_lock(&mutex);
while (!server_ready)
(void) pthread_cond_wait(&cv, &mutex);
server_ready = _B_FALSE;
(void) pthread_mutex_unlock(&mutex);
clienttest(t);
/* Tell the server we are done */
(void) pthread_mutex_lock(&cmutex);
client_done = _B_TRUE;
(void) pthread_cond_signal(&ccv);
(void) pthread_mutex_unlock(&cmutex);
}
return (NULL);
}
int
main(int argc, const char **argv)
{
int ret = EXIT_SUCCESS;
pthread_t cthread;
if (argc > 1 && strcmp(argv[1], "-d") == 0) {
debug = _B_TRUE;
argc--, argv++;
}
/* -c <server IP> <test name> */
if (argc == 4 && strcmp(argv[1], "-c") == 0) {
testip = inet_addr(argv[2]);
printf("TEST IP: %s\n", argv[2]);
if (testip == INADDR_NONE) {
err(EXIT_FAILURE,
"Could not parse destination IP address");
}
client((void *)argv[3]);
return (ret);
}
/* -s <test name> */
if (argc == 3 && strcmp(argv[1], "-s") == 0)
return (server(argv[2]));
testip = inet_addr("127.0.0.1");
if (testip == INADDR_NONE)
err(EXIT_FAILURE, "Could not parse destination IP address");
if (pthread_create(&cthread, NULL, client, NULL) == -1)
err(EXIT_FAILURE, "Could not create client thread");
ret = server(NULL);
if (pthread_join(cthread, NULL) != 0)
err(EXIT_FAILURE, "join client thread failed");
return (ret);
}
|