ipv6_fragmentation.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: Brett A C Sheffield <bacs@librecast.net>
  4. *
  5. * Kernel selftest for the IPv6 fragmentation regression which affected stable
  6. * kernels:
  7. *
  8. * https://lore.kernel.org/stable/aElivdUXqd1OqgMY@karahi.gladserv.com
  9. *
  10. * Commit: a18dfa9925b9 ("ipv6: save dontfrag in cork") was backported to stable
  11. * without some prerequisite commits.
  12. *
  13. * This caused a regression when sending IPv6 UDP packets by preventing
  14. * fragmentation and instead returning -1 (EMSGSIZE).
  15. *
  16. * This selftest demonstrates the issue by sending an IPv6 UDP packet to
  17. * localhost (::1) on the loopback interface from the autoconfigured link-local
  18. * address.
  19. *
  20. * sendmsg(2) returns bytes sent correctly on a working kernel, and returns -1
  21. * (EMSGSIZE) when the regression is present.
  22. *
  23. * The regression was not present in the mainline kernel, but add this test to
  24. * catch similar breakage in future.
  25. */
  26. #define _GNU_SOURCE
  27. #include <error.h>
  28. #include <net/if.h>
  29. #include <netinet/in.h>
  30. #include <sched.h>
  31. #include <stdio.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/socket.h>
  34. #include <unistd.h>
  35. #include "kselftest.h"
  36. #define MTU 1500
  37. #define LARGER_THAN_MTU 8192
  38. static void setup(void)
  39. {
  40. struct ifreq ifr = {
  41. .ifr_name = "lo"
  42. };
  43. int ctl;
  44. /* we need to set MTU, so do this in a namespace to play nicely */
  45. if (unshare(CLONE_NEWNET) == -1)
  46. error(KSFT_FAIL, errno, "unshare");
  47. ctl = socket(AF_LOCAL, SOCK_STREAM, 0);
  48. if (ctl == -1)
  49. error(KSFT_FAIL, errno, "socket");
  50. /* ensure MTU is smaller than what we plan to send */
  51. ifr.ifr_mtu = MTU;
  52. if (ioctl(ctl, SIOCSIFMTU, &ifr) == -1)
  53. error(KSFT_FAIL, errno, "ioctl: set MTU");
  54. /* bring up interface */
  55. if (ioctl(ctl, SIOCGIFFLAGS, &ifr) == -1)
  56. error(KSFT_FAIL, errno, "ioctl SIOCGIFFLAGS");
  57. ifr.ifr_flags = ifr.ifr_flags | IFF_UP;
  58. if (ioctl(ctl, SIOCSIFFLAGS, &ifr) == -1)
  59. error(KSFT_FAIL, errno, "ioctl: bring interface up");
  60. if (close(ctl) == -1)
  61. error(KSFT_FAIL, errno, "close");
  62. }
  63. int main(void)
  64. {
  65. struct in6_addr addr = {
  66. .s6_addr[15] = 0x01, /* ::1 */
  67. };
  68. struct sockaddr_in6 sa = {
  69. .sin6_family = AF_INET6,
  70. .sin6_addr = addr,
  71. .sin6_port = htons(9) /* port 9/udp (DISCARD) */
  72. };
  73. static char buf[LARGER_THAN_MTU] = {0};
  74. struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
  75. struct msghdr msg = {
  76. .msg_iov = &iov,
  77. .msg_iovlen = 1,
  78. .msg_name = (struct sockaddr *)&sa,
  79. .msg_namelen = sizeof(sa),
  80. };
  81. ssize_t rc;
  82. int s;
  83. printf("Testing IPv6 fragmentation\n");
  84. setup();
  85. s = socket(AF_INET6, SOCK_DGRAM, 0);
  86. send_again:
  87. rc = sendmsg(s, &msg, 0);
  88. if (rc == -1) {
  89. /* if interface wasn't ready, try again */
  90. if (errno == EADDRNOTAVAIL) {
  91. usleep(1000);
  92. goto send_again;
  93. }
  94. error(KSFT_FAIL, errno, "sendmsg");
  95. } else if (rc != LARGER_THAN_MTU) {
  96. error(KSFT_FAIL, errno, "sendmsg returned %zi, expected %i",
  97. rc, LARGER_THAN_MTU);
  98. }
  99. printf("[PASS] sendmsg() returned %zi\n", rc);
  100. if (close(s) == -1)
  101. error(KSFT_FAIL, errno, "close");
  102. return KSFT_PASS;
  103. }