1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright Amazon.com Inc. or its affiliates. */
8 #include <netinet/in.h>
10 #include <sys/ioctl.h>
11 #include <sys/signalfd.h>
12 #include <sys/socket.h>
14 #include "../../kselftest_harness.h"
20 int fd[4]; /* 0: AF_UNIX sender
26 int epoll_fd[2]; /* 0: AF_UNIX receiver
32 FIXTURE_VARIANT(msg_oob)
37 FIXTURE_VARIANT_ADD(msg_oob, no_peek)
42 FIXTURE_VARIANT_ADD(msg_oob, peek)
47 static void create_unix_socketpair(struct __test_metadata *_metadata,
48 FIXTURE_DATA(msg_oob) *self)
52 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, self->fd);
56 static void create_tcp_socketpair(struct __test_metadata *_metadata,
57 FIXTURE_DATA(msg_oob) *self)
59 struct sockaddr_in addr;
64 listen_fd = socket(AF_INET, SOCK_STREAM, 0);
65 ASSERT_GE(listen_fd, 0);
67 ret = listen(listen_fd, -1);
70 addrlen = sizeof(addr);
71 ret = getsockname(listen_fd, (struct sockaddr *)&addr, &addrlen);
74 self->fd[2] = socket(AF_INET, SOCK_STREAM, 0);
75 ASSERT_GE(self->fd[2], 0);
77 ret = connect(self->fd[2], (struct sockaddr *)&addr, addrlen);
80 self->fd[3] = accept(listen_fd, (struct sockaddr *)&addr, &addrlen);
81 ASSERT_GE(self->fd[3], 0);
83 ret = fcntl(self->fd[3], F_SETFL, O_NONBLOCK);
87 static void setup_sigurg(struct __test_metadata *_metadata,
88 FIXTURE_DATA(msg_oob) *self)
90 struct signalfd_siginfo siginfo;
95 for (i = 0; i < 2; i++) {
96 ret = ioctl(self->fd[i * 2 + 1], FIOSETOWN, &pid);
100 ret = sigemptyset(&mask);
103 ret = sigaddset(&mask, SIGURG);
106 ret = sigprocmask(SIG_BLOCK, &mask, NULL);
109 self->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK);
110 ASSERT_GE(self->signal_fd, 0);
112 ret = read(self->signal_fd, &siginfo, sizeof(siginfo));
116 static void setup_epollpri(struct __test_metadata *_metadata,
117 FIXTURE_DATA(msg_oob) *self)
119 struct epoll_event event = {
124 for (i = 0; i < 2; i++) {
127 self->epoll_fd[i] = epoll_create1(0);
128 ASSERT_GE(self->epoll_fd[i], 0);
130 ret = epoll_ctl(self->epoll_fd[i], EPOLL_CTL_ADD, self->fd[i * 2 + 1], &event);
135 static void close_sockets(FIXTURE_DATA(msg_oob) *self)
139 for (i = 0; i < 4; i++)
143 FIXTURE_SETUP(msg_oob)
145 create_unix_socketpair(_metadata, self);
146 create_tcp_socketpair(_metadata, self);
148 setup_sigurg(_metadata, self);
149 setup_epollpri(_metadata, self);
151 self->tcp_compliant = true;
154 FIXTURE_TEARDOWN(msg_oob)
159 static void __epollpair(struct __test_metadata *_metadata,
160 FIXTURE_DATA(msg_oob) *self,
163 struct epoll_event event[2] = {};
166 for (i = 0; i < 2; i++)
167 ret[i] = epoll_wait(self->epoll_fd[i], &event[i], 1, 0);
169 ASSERT_EQ(ret[0], oob_remaining);
171 if (self->tcp_compliant)
172 ASSERT_EQ(ret[0], ret[1]);
175 ASSERT_EQ(event[0].events, EPOLLPRI);
177 if (self->tcp_compliant)
178 ASSERT_EQ(event[0].events, event[1].events);
182 static void __sendpair(struct __test_metadata *_metadata,
183 FIXTURE_DATA(msg_oob) *self,
184 const void *buf, size_t len, int flags)
188 for (i = 0; i < 2; i++) {
189 struct signalfd_siginfo siginfo = {};
192 ret[i] = send(self->fd[i * 2], buf, len, flags);
194 bytes = read(self->signal_fd, &siginfo, sizeof(siginfo));
196 if (flags & MSG_OOB) {
197 ASSERT_EQ(bytes, sizeof(siginfo));
198 ASSERT_EQ(siginfo.ssi_signo, SIGURG);
200 bytes = read(self->signal_fd, &siginfo, sizeof(siginfo));
203 ASSERT_EQ(bytes, -1);
206 ASSERT_EQ(ret[0], len);
207 ASSERT_EQ(ret[0], ret[1]);
210 static void __recvpair(struct __test_metadata *_metadata,
211 FIXTURE_DATA(msg_oob) *self,
212 const char *expected_buf, int expected_len,
213 int buf_len, int flags)
215 int i, ret[2], recv_errno[2], expected_errno = 0;
216 char recv_buf[2][BUF_SZ] = {};
217 bool printed = false;
219 ASSERT_GE(BUF_SZ, buf_len);
223 for (i = 0; i < 2; i++) {
224 ret[i] = recv(self->fd[i * 2 + 1], recv_buf[i], buf_len, flags);
225 recv_errno[i] = errno;
228 if (expected_len < 0) {
229 expected_errno = -expected_len;
233 if (ret[0] != expected_len || recv_errno[0] != expected_errno) {
234 TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
235 TH_LOG("Expected:%s", expected_errno ? strerror(expected_errno) : expected_buf);
237 ASSERT_EQ(ret[0], expected_len);
238 ASSERT_EQ(recv_errno[0], expected_errno);
241 if (ret[0] != ret[1] || recv_errno[0] != recv_errno[1]) {
242 TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
243 TH_LOG("TCP :%s", ret[1] < 0 ? strerror(recv_errno[1]) : recv_buf[1]);
247 if (self->tcp_compliant) {
248 ASSERT_EQ(ret[0], ret[1]);
249 ASSERT_EQ(recv_errno[0], recv_errno[1]);
253 if (expected_len >= 0) {
256 cmp = strncmp(expected_buf, recv_buf[0], expected_len);
258 TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
259 TH_LOG("Expected:%s", expected_errno ? strerror(expected_errno) : expected_buf);
264 cmp = strncmp(recv_buf[0], recv_buf[1], expected_len);
267 TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
268 TH_LOG("TCP :%s", ret[1] < 0 ? strerror(recv_errno[1]) : recv_buf[1]);
271 if (self->tcp_compliant)
277 static void __setinlinepair(struct __test_metadata *_metadata,
278 FIXTURE_DATA(msg_oob) *self)
280 int i, oob_inline = 1;
282 for (i = 0; i < 2; i++) {
285 ret = setsockopt(self->fd[i * 2 + 1], SOL_SOCKET, SO_OOBINLINE,
286 &oob_inline, sizeof(oob_inline));
291 static void __siocatmarkpair(struct __test_metadata *_metadata,
292 FIXTURE_DATA(msg_oob) *self,
298 for (i = 0; i < 2; i++) {
301 ret = ioctl(self->fd[i * 2 + 1], SIOCATMARK, &answ[i]);
305 ASSERT_EQ(answ[0], oob_head);
307 if (self->tcp_compliant)
308 ASSERT_EQ(answ[0], answ[1]);
311 #define sendpair(buf, len, flags) \
312 __sendpair(_metadata, self, buf, len, flags)
314 #define recvpair(expected_buf, expected_len, buf_len, flags) \
317 __recvpair(_metadata, self, \
318 expected_buf, expected_len, \
319 buf_len, (flags) | MSG_PEEK); \
320 __recvpair(_metadata, self, \
321 expected_buf, expected_len, buf_len, flags); \
324 #define epollpair(oob_remaining) \
325 __epollpair(_metadata, self, oob_remaining)
327 #define siocatmarkpair(oob_head) \
328 __siocatmarkpair(_metadata, self, oob_head)
330 #define setinlinepair() \
331 __setinlinepair(_metadata, self)
333 #define tcp_incompliant \
334 for (self->tcp_compliant = false; \
335 self->tcp_compliant == false; \
336 self->tcp_compliant = true)
338 TEST_F(msg_oob, non_oob)
342 siocatmarkpair(false);
344 recvpair("", -EINVAL, 1, MSG_OOB);
346 siocatmarkpair(false);
351 sendpair("x", 1, MSG_OOB);
353 siocatmarkpair(true);
355 recvpair("x", 1, 1, MSG_OOB);
357 siocatmarkpair(true);
360 TEST_F(msg_oob, oob_drop)
362 sendpair("x", 1, MSG_OOB);
364 siocatmarkpair(true);
366 recvpair("", -EAGAIN, 1, 0); /* Drop OOB. */
368 siocatmarkpair(false);
370 recvpair("", -EINVAL, 1, MSG_OOB);
372 siocatmarkpair(false);
375 TEST_F(msg_oob, oob_ahead)
377 sendpair("hello", 5, MSG_OOB);
379 siocatmarkpair(false);
381 recvpair("o", 1, 1, MSG_OOB);
383 siocatmarkpair(false);
385 recvpair("hell", 4, 4, 0);
387 siocatmarkpair(true);
390 TEST_F(msg_oob, oob_break)
392 sendpair("hello", 5, MSG_OOB);
394 siocatmarkpair(false);
396 recvpair("hell", 4, 5, 0); /* Break at OOB even with enough buffer. */
398 siocatmarkpair(true);
400 recvpair("o", 1, 1, MSG_OOB);
402 siocatmarkpair(true);
404 recvpair("", -EAGAIN, 1, 0);
405 siocatmarkpair(false);
408 TEST_F(msg_oob, oob_ahead_break)
410 sendpair("hello", 5, MSG_OOB);
412 siocatmarkpair(false);
414 sendpair("world", 5, 0);
416 siocatmarkpair(false);
418 recvpair("o", 1, 1, MSG_OOB);
420 siocatmarkpair(false);
422 recvpair("hell", 4, 9, 0); /* Break at OOB even after it's recv()ed. */
424 siocatmarkpair(true);
426 recvpair("world", 5, 5, 0);
428 siocatmarkpair(false);
431 TEST_F(msg_oob, oob_break_drop)
433 sendpair("hello", 5, MSG_OOB);
435 siocatmarkpair(false);
437 sendpair("world", 5, 0);
439 siocatmarkpair(false);
441 recvpair("hell", 4, 10, 0); /* Break at OOB even with enough buffer. */
443 siocatmarkpair(true);
445 recvpair("world", 5, 10, 0); /* Drop OOB and recv() the next skb. */
447 siocatmarkpair(false);
449 recvpair("", -EINVAL, 1, MSG_OOB);
451 siocatmarkpair(false);
454 TEST_F(msg_oob, ex_oob_break)
456 sendpair("hello", 5, MSG_OOB);
458 siocatmarkpair(false);
460 sendpair("wor", 3, MSG_OOB);
462 siocatmarkpair(false);
464 sendpair("ld", 2, 0);
466 siocatmarkpair(false);
468 recvpair("hellowo", 7, 10, 0); /* Break at OOB but not at ex-OOB. */
470 siocatmarkpair(true);
472 recvpair("r", 1, 1, MSG_OOB);
474 siocatmarkpair(true);
476 recvpair("ld", 2, 2, 0);
478 siocatmarkpair(false);
481 TEST_F(msg_oob, ex_oob_drop)
483 sendpair("x", 1, MSG_OOB);
485 siocatmarkpair(true);
487 sendpair("y", 1, MSG_OOB); /* TCP drops "x" at this moment. */
491 siocatmarkpair(false);
493 recvpair("x", 1, 1, 0); /* TCP drops "y" by passing through it. */
495 siocatmarkpair(true);
497 recvpair("y", 1, 1, MSG_OOB); /* TCP returns -EINVAL. */
499 siocatmarkpair(true);
503 TEST_F(msg_oob, ex_oob_drop_2)
505 sendpair("x", 1, MSG_OOB);
507 siocatmarkpair(true);
509 sendpair("y", 1, MSG_OOB); /* TCP drops "x" at this moment. */
513 siocatmarkpair(false);
516 recvpair("y", 1, 1, MSG_OOB);
520 siocatmarkpair(false);
522 recvpair("x", 1, 1, 0); /* TCP returns -EAGAIN. */
524 siocatmarkpair(true);
528 TEST_F(msg_oob, ex_oob_oob)
530 sendpair("x", 1, MSG_OOB);
532 siocatmarkpair(true);
534 recvpair("x", 1, 1, MSG_OOB);
536 siocatmarkpair(true);
538 sendpair("y", 1, MSG_OOB);
540 siocatmarkpair(true);
542 recvpair("", -EAGAIN, 1, 0);
544 siocatmarkpair(false);
546 recvpair("", -EINVAL, 1, MSG_OOB);
548 siocatmarkpair(false);
551 TEST_F(msg_oob, ex_oob_ahead_break)
553 sendpair("hello", 5, MSG_OOB);
555 siocatmarkpair(false);
557 sendpair("wor", 3, MSG_OOB);
559 siocatmarkpair(false);
561 recvpair("r", 1, 1, MSG_OOB);
563 siocatmarkpair(false);
565 sendpair("ld", 2, MSG_OOB);
567 siocatmarkpair(false);
570 recvpair("hellowol", 8, 10, 0); /* TCP recv()s "helloworl", why "r" ?? */
574 siocatmarkpair(true);
576 recvpair("d", 1, 1, MSG_OOB);
578 siocatmarkpair(true);
581 TEST_F(msg_oob, ex_oob_siocatmark)
583 sendpair("hello", 5, MSG_OOB);
585 siocatmarkpair(false);
587 recvpair("o", 1, 1, MSG_OOB);
589 siocatmarkpair(false);
591 sendpair("world", 5, MSG_OOB);
593 siocatmarkpair(false);
595 recvpair("hell", 4, 4, 0); /* Intentionally stop at ex-OOB. */
597 siocatmarkpair(false);
600 TEST_F(msg_oob, inline_oob)
604 sendpair("x", 1, MSG_OOB);
606 siocatmarkpair(true);
608 recvpair("", -EINVAL, 1, MSG_OOB);
610 siocatmarkpair(true);
612 recvpair("x", 1, 1, 0);
614 siocatmarkpair(false);
617 TEST_F(msg_oob, inline_oob_break)
621 sendpair("hello", 5, MSG_OOB);
623 siocatmarkpair(false);
625 recvpair("", -EINVAL, 1, MSG_OOB);
627 siocatmarkpair(false);
629 recvpair("hell", 4, 5, 0); /* Break at OOB but not at ex-OOB. */
631 siocatmarkpair(true);
633 recvpair("o", 1, 1, 0);
635 siocatmarkpair(false);
638 TEST_F(msg_oob, inline_oob_ahead_break)
640 sendpair("hello", 5, MSG_OOB);
642 siocatmarkpair(false);
644 sendpair("world", 5, 0);
646 siocatmarkpair(false);
648 recvpair("o", 1, 1, MSG_OOB);
650 siocatmarkpair(false);
654 recvpair("hell", 4, 9, 0); /* Break at OOB even with enough buffer. */
656 siocatmarkpair(true);
659 recvpair("world", 5, 6, 0); /* TCP recv()s "oworld", ... "o" ??? */
663 siocatmarkpair(false);
666 TEST_F(msg_oob, inline_ex_oob_break)
668 sendpair("hello", 5, MSG_OOB);
670 siocatmarkpair(false);
672 sendpair("wor", 3, MSG_OOB);
674 siocatmarkpair(false);
676 sendpair("ld", 2, 0);
678 siocatmarkpair(false);
682 recvpair("hellowo", 7, 10, 0); /* Break at OOB but not at ex-OOB. */
684 siocatmarkpair(true);
686 recvpair("rld", 3, 3, 0);
688 siocatmarkpair(false);
691 TEST_F(msg_oob, inline_ex_oob_no_drop)
693 sendpair("x", 1, MSG_OOB);
695 siocatmarkpair(true);
699 sendpair("y", 1, MSG_OOB); /* TCP does NOT drops "x" at this moment. */
701 siocatmarkpair(false);
703 recvpair("x", 1, 1, 0);
705 siocatmarkpair(true);
707 recvpair("y", 1, 1, 0);
709 siocatmarkpair(false);
712 TEST_F(msg_oob, inline_ex_oob_drop)
714 sendpair("x", 1, MSG_OOB);
716 siocatmarkpair(true);
718 sendpair("y", 1, MSG_OOB); /* TCP drops "x" at this moment. */
724 siocatmarkpair(false);
726 recvpair("x", 1, 1, 0); /* TCP recv()s "y". */
728 siocatmarkpair(true);
730 recvpair("y", 1, 1, 0); /* TCP returns -EAGAIN. */
732 siocatmarkpair(false);
736 TEST_F(msg_oob, inline_ex_oob_siocatmark)
738 sendpair("hello", 5, MSG_OOB);
740 siocatmarkpair(false);
742 recvpair("o", 1, 1, MSG_OOB);
744 siocatmarkpair(false);
748 sendpair("world", 5, MSG_OOB);
750 siocatmarkpair(false);
752 recvpair("hell", 4, 4, 0); /* Intentionally stop at ex-OOB. */
754 siocatmarkpair(false);