sk_so_peek_off.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include "kselftest.h"
  11. static char *afstr(int af, int proto)
  12. {
  13. if (proto == IPPROTO_TCP)
  14. return af == AF_INET ? "TCP/IPv4" : "TCP/IPv6";
  15. else
  16. return af == AF_INET ? "UDP/IPv4" : "UDP/IPv6";
  17. }
  18. int sk_peek_offset_probe(sa_family_t af, int proto)
  19. {
  20. int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM);
  21. int optv = 0;
  22. int ret = 0;
  23. int s;
  24. s = socket(af, type, proto);
  25. if (s < 0) {
  26. ksft_perror("Temporary TCP socket creation failed");
  27. } else {
  28. if (!setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &optv, sizeof(int)))
  29. ret = 1;
  30. else
  31. printf("%s does not support SO_PEEK_OFF\n", afstr(af, proto));
  32. close(s);
  33. }
  34. return ret;
  35. }
  36. static void sk_peek_offset_set(int s, int offset)
  37. {
  38. if (setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &offset, sizeof(offset)))
  39. ksft_perror("Failed to set SO_PEEK_OFF value\n");
  40. }
  41. static int sk_peek_offset_get(int s)
  42. {
  43. int offset;
  44. socklen_t len = sizeof(offset);
  45. if (getsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &offset, &len))
  46. ksft_perror("Failed to get SO_PEEK_OFF value\n");
  47. return offset;
  48. }
  49. static int sk_peek_offset_test(sa_family_t af, int proto)
  50. {
  51. int type = (proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM);
  52. union {
  53. struct sockaddr sa;
  54. struct sockaddr_in a4;
  55. struct sockaddr_in6 a6;
  56. } a;
  57. int res = 0;
  58. int s[2] = {0, 0};
  59. int recv_sock = 0;
  60. int offset = 0;
  61. ssize_t len;
  62. char buf[2];
  63. memset(&a, 0, sizeof(a));
  64. a.sa.sa_family = af;
  65. s[0] = recv_sock = socket(af, type, proto);
  66. s[1] = socket(af, type, proto);
  67. if (s[0] < 0 || s[1] < 0) {
  68. ksft_perror("Temporary socket creation failed\n");
  69. goto out;
  70. }
  71. if (bind(s[0], &a.sa, sizeof(a)) < 0) {
  72. ksft_perror("Temporary socket bind() failed\n");
  73. goto out;
  74. }
  75. if (getsockname(s[0], &a.sa, &((socklen_t) { sizeof(a) })) < 0) {
  76. ksft_perror("Temporary socket getsockname() failed\n");
  77. goto out;
  78. }
  79. if (proto == IPPROTO_TCP && listen(s[0], 0) < 0) {
  80. ksft_perror("Temporary socket listen() failed\n");
  81. goto out;
  82. }
  83. if (connect(s[1], &a.sa, sizeof(a)) < 0) {
  84. ksft_perror("Temporary socket connect() failed\n");
  85. goto out;
  86. }
  87. if (proto == IPPROTO_TCP) {
  88. recv_sock = accept(s[0], NULL, NULL);
  89. if (recv_sock <= 0) {
  90. ksft_perror("Temporary socket accept() failed\n");
  91. goto out;
  92. }
  93. }
  94. /* Some basic tests of getting/setting offset */
  95. offset = sk_peek_offset_get(recv_sock);
  96. if (offset != -1) {
  97. ksft_perror("Initial value of socket offset not -1\n");
  98. goto out;
  99. }
  100. sk_peek_offset_set(recv_sock, 0);
  101. offset = sk_peek_offset_get(recv_sock);
  102. if (offset != 0) {
  103. ksft_perror("Failed to set socket offset to 0\n");
  104. goto out;
  105. }
  106. /* Transfer a message */
  107. if (send(s[1], (char *)("ab"), 2, 0) != 2) {
  108. ksft_perror("Temporary probe socket send() failed\n");
  109. goto out;
  110. }
  111. /* Read first byte */
  112. len = recv(recv_sock, buf, 1, MSG_PEEK);
  113. if (len != 1 || buf[0] != 'a') {
  114. ksft_perror("Failed to read first byte of message\n");
  115. goto out;
  116. }
  117. offset = sk_peek_offset_get(recv_sock);
  118. if (offset != 1) {
  119. ksft_perror("Offset not forwarded correctly at first byte\n");
  120. goto out;
  121. }
  122. /* Try to read beyond last byte */
  123. len = recv(recv_sock, buf, 2, MSG_PEEK);
  124. if (len != 1 || buf[0] != 'b') {
  125. ksft_perror("Failed to read last byte of message\n");
  126. goto out;
  127. }
  128. offset = sk_peek_offset_get(recv_sock);
  129. if (offset != 2) {
  130. ksft_perror("Offset not forwarded correctly at last byte\n");
  131. goto out;
  132. }
  133. /* Flush message */
  134. len = recv(recv_sock, buf, 2, MSG_TRUNC);
  135. if (len != 2) {
  136. ksft_perror("Failed to flush message\n");
  137. goto out;
  138. }
  139. offset = sk_peek_offset_get(recv_sock);
  140. if (offset != 0) {
  141. ksft_perror("Offset not reverted correctly after flush\n");
  142. goto out;
  143. }
  144. printf("%s with MSG_PEEK_OFF works correctly\n", afstr(af, proto));
  145. res = 1;
  146. out:
  147. if (proto == IPPROTO_TCP && recv_sock >= 0)
  148. close(recv_sock);
  149. if (s[1] >= 0)
  150. close(s[1]);
  151. if (s[0] >= 0)
  152. close(s[0]);
  153. return res;
  154. }
  155. static int do_test(int proto)
  156. {
  157. int res4, res6;
  158. res4 = sk_peek_offset_probe(AF_INET, proto);
  159. res6 = sk_peek_offset_probe(AF_INET6, proto);
  160. if (!res4 && !res6)
  161. return KSFT_SKIP;
  162. if (res4)
  163. res4 = sk_peek_offset_test(AF_INET, proto);
  164. if (res6)
  165. res6 = sk_peek_offset_test(AF_INET6, proto);
  166. if (!res4 || !res6)
  167. return KSFT_FAIL;
  168. return KSFT_PASS;
  169. }
  170. int main(void)
  171. {
  172. int restcp, resudp;
  173. restcp = do_test(IPPROTO_TCP);
  174. resudp = do_test(IPPROTO_UDP);
  175. if (restcp == KSFT_FAIL || resudp == KSFT_FAIL)
  176. return KSFT_FAIL;
  177. return KSFT_PASS;
  178. }