check_native.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Determine whether interfaces use native transport. Linux version.
  2. Copyright (C) 2007-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <ifaddrs.h>
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <net/if.h>
  25. #include <net/if_arp.h>
  26. #include <sys/ioctl.h>
  27. #include <asm/types.h>
  28. #include <linux/netlink.h>
  29. #include <linux/rtnetlink.h>
  30. #include <not-cancel.h>
  31. #include "netlinkaccess.h"
  32. void
  33. __check_native (uint32_t a1_index, int *a1_native,
  34. uint32_t a2_index, int *a2_native)
  35. {
  36. int fd = __socket (PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
  37. struct sockaddr_nl nladdr;
  38. memset (&nladdr, '\0', sizeof (nladdr));
  39. nladdr.nl_family = AF_NETLINK;
  40. socklen_t addr_len = sizeof (nladdr);
  41. if (fd < 0
  42. || __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0
  43. || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0)
  44. return;
  45. pid_t pid = nladdr.nl_pid;
  46. struct req
  47. {
  48. struct nlmsghdr nlh;
  49. struct rtgenmsg g;
  50. /* struct rtgenmsg consists of a single byte. This means there
  51. are three bytes of padding included in the REQ definition.
  52. We make them explicit here. */
  53. char pad[3];
  54. } req;
  55. req.nlh.nlmsg_len = sizeof (req);
  56. req.nlh.nlmsg_type = RTM_GETLINK;
  57. req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
  58. req.nlh.nlmsg_pid = 0;
  59. req.nlh.nlmsg_seq = time (NULL);
  60. req.g.rtgen_family = AF_UNSPEC;
  61. assert (sizeof (req) - offsetof (struct req, pad) == 3);
  62. memset (req.pad, '\0', sizeof (req.pad));
  63. memset (&nladdr, '\0', sizeof (nladdr));
  64. nladdr.nl_family = AF_NETLINK;
  65. #ifdef PAGE_SIZE
  66. /* Help the compiler optimize out the malloc call if PAGE_SIZE
  67. is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
  68. const size_t buf_size = PAGE_SIZE;
  69. #else
  70. const size_t buf_size = __getpagesize ();
  71. #endif
  72. bool use_malloc = false;
  73. char *buf;
  74. if (__libc_use_alloca (buf_size))
  75. buf = alloca (buf_size);
  76. else
  77. {
  78. buf = malloc (buf_size);
  79. if (buf != NULL)
  80. use_malloc = true;
  81. else
  82. goto out_fail;
  83. }
  84. struct iovec iov = { buf, buf_size };
  85. if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
  86. (struct sockaddr *) &nladdr,
  87. sizeof (nladdr))) < 0)
  88. goto out_fail;
  89. bool done = false;
  90. do
  91. {
  92. struct msghdr msg =
  93. {
  94. .msg_name = (void *) &nladdr,
  95. .msg_namelen = sizeof (nladdr),
  96. .msg_iov = &iov,
  97. .msg_iovlen = 1,
  98. .msg_control = NULL,
  99. .msg_controllen = 0,
  100. .msg_flags = 0
  101. };
  102. ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
  103. __netlink_assert_response (fd, read_len);
  104. if (read_len < 0)
  105. goto out_fail;
  106. if (msg.msg_flags & MSG_TRUNC)
  107. goto out_fail;
  108. struct nlmsghdr *nlmh;
  109. for (nlmh = (struct nlmsghdr *) buf;
  110. NLMSG_OK (nlmh, (size_t) read_len);
  111. nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
  112. {
  113. if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
  114. || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
  115. continue;
  116. if (nlmh->nlmsg_type == RTM_NEWLINK)
  117. {
  118. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlmh);
  119. int native = (ifim->ifi_type != ARPHRD_TUNNEL6
  120. && ifim->ifi_type != ARPHRD_TUNNEL
  121. && ifim->ifi_type != ARPHRD_SIT);
  122. if (a1_index == ifim->ifi_index)
  123. {
  124. *a1_native = native;
  125. a1_index = 0xffffffffu;
  126. }
  127. if (a2_index == ifim->ifi_index)
  128. {
  129. *a2_native = native;
  130. a2_index = 0xffffffffu;
  131. }
  132. if (a1_index == 0xffffffffu
  133. && a2_index == 0xffffffffu)
  134. goto out;
  135. }
  136. else if (nlmh->nlmsg_type == NLMSG_DONE)
  137. /* We found the end, leave the loop. */
  138. done = true;
  139. }
  140. }
  141. while (! done);
  142. out:
  143. __close_nocancel_nostatus (fd);
  144. return;
  145. out_fail:
  146. if (use_malloc)
  147. free (buf);
  148. }