NetPlatform_linux.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/platform/netdev/NetPlatform.h"
  16. #define string_strncpy
  17. #define string_strlen
  18. #define string_strerror
  19. #include "util/platform/libc/string.h"
  20. #include "util/platform/Sockaddr.h"
  21. #include "util/Bits.h"
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <sys/ioctl.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <sys/socket.h>
  28. #include <sys/types.h>
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <net/if.h>
  32. #include <arpa/inet.h>
  33. #include <linux/if.h>
  34. /**
  35. * This hack exists because linux/in.h and linux/in6.h define
  36. * the same structures, leading to redefinition errors.
  37. */
  38. #ifndef _LINUX_IN6_H
  39. struct in6_ifreq
  40. {
  41. struct in6_addr ifr6_addr;
  42. uint32_t ifr6_prefixlen;
  43. int ifr6_ifindex;
  44. };
  45. #endif
  46. /**
  47. * Get a socket and ifRequest for a given interface by name.
  48. *
  49. * @param interfaceName the name of the interface, eg: tun0
  50. * @param af either AF_INET or AF_INET6
  51. * @param eg an exception handler in case something goes wrong.
  52. * this will send a -1 for all errors.
  53. * @param ifRequestOut an ifreq which will be populated with the interface index of the interface.
  54. * @return a socket for interacting with this interface.
  55. */
  56. static int socketForIfName(const char* interfaceName,
  57. int af,
  58. struct Except* eh,
  59. struct ifreq* ifRequestOut)
  60. {
  61. int s;
  62. if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
  63. Except_throw(eh, "socket() [%s]", strerror(errno));
  64. }
  65. Bits_memset(ifRequestOut, 0, sizeof(struct ifreq));
  66. strncpy(ifRequestOut->ifr_name, interfaceName, IFNAMSIZ);
  67. if (ioctl(s, SIOCGIFINDEX, ifRequestOut) < 0) {
  68. int err = errno;
  69. close(s);
  70. Except_throw(eh, "ioctl(SIOCGIFINDEX) [%s]", strerror(err));
  71. }
  72. return s;
  73. }
  74. static void checkInterfaceUp(int socket,
  75. struct ifreq* ifRequest,
  76. struct Log* logger,
  77. struct Except* eh)
  78. {
  79. if (ioctl(socket, SIOCGIFFLAGS, ifRequest) < 0) {
  80. int err = errno;
  81. close(socket);
  82. Except_throw(eh, "ioctl(SIOCGIFFLAGS) [%s]", strerror(err));
  83. }
  84. if (ifRequest->ifr_flags & IFF_UP & IFF_RUNNING) {
  85. // already up.
  86. return;
  87. }
  88. Log_info(logger, "Bringing up interface [%s]", ifRequest->ifr_name);
  89. ifRequest->ifr_flags |= IFF_UP | IFF_RUNNING;
  90. if (ioctl(socket, SIOCSIFFLAGS, ifRequest) < 0) {
  91. int err = errno;
  92. close(socket);
  93. Except_throw(eh, "ioctl(SIOCSIFFLAGS) [%s]", strerror(err));
  94. }
  95. }
  96. void NetPlatform_addAddress(const char* interfaceName,
  97. const uint8_t* address,
  98. int prefixLen,
  99. int addrFam,
  100. struct Log* logger,
  101. struct Except* eh)
  102. {
  103. struct ifreq ifRequest;
  104. int s = socketForIfName(interfaceName, addrFam, eh, &ifRequest);
  105. int ifIndex = ifRequest.ifr_ifindex;
  106. // checkInterfaceUp() clobbers the ifindex.
  107. checkInterfaceUp(s, &ifRequest, logger, eh);
  108. if (addrFam == Sockaddr_AF_INET6) {
  109. struct in6_ifreq ifr6 = {
  110. .ifr6_ifindex = ifIndex,
  111. .ifr6_prefixlen = prefixLen
  112. };
  113. Bits_memcpyConst(&ifr6.ifr6_addr, address, 16);
  114. if (ioctl(s, SIOCSIFADDR, &ifr6) < 0) {
  115. int err = errno;
  116. close(s);
  117. Except_throw(eh, "ioctl(SIOCSIFADDR) [%s]", strerror(err));
  118. }
  119. } else if (addrFam == Sockaddr_AF_INET) {
  120. struct sockaddr_in sin = { .sin_family = AF_INET, .sin_port = 0 };
  121. Bits_memcpyConst(&sin.sin_addr.s_addr, address, 4);
  122. Bits_memcpyConst(&ifRequest.ifr_addr, &sin, sizeof(struct sockaddr));
  123. if (ioctl(s, SIOCSIFADDR, &ifRequest) < 0) {
  124. int err = errno;
  125. close(s);
  126. Except_throw(eh, "ioctl(SIOCSIFADDR) failed: [%s]", strerror(err));
  127. }
  128. uint32_t x = ~0 << (32 - prefixLen);
  129. x = Endian_hostToBigEndian32(x);
  130. Bits_memcpyConst(&sin.sin_addr, &x, 4);
  131. Bits_memcpyConst(&ifRequest.ifr_addr, &sin, sizeof(struct sockaddr_in));
  132. if (ioctl(s, SIOCSIFNETMASK, &ifRequest) < 0) {
  133. int err = errno;
  134. close(s);
  135. Except_throw(eh, "ioctl(SIOCSIFNETMASK) failed: [%s]", strerror(err));
  136. }
  137. } else {
  138. Assert_always(0);
  139. }
  140. close(s);
  141. }
  142. void NetPlatform_setMTU(const char* interfaceName,
  143. uint32_t mtu,
  144. struct Log* logger,
  145. struct Except* eh)
  146. {
  147. struct ifreq ifRequest;
  148. int s = socketForIfName(interfaceName, AF_INET6, eh, &ifRequest);
  149. Log_info(logger, "Setting MTU for device [%s] to [%u] bytes.", interfaceName, mtu);
  150. ifRequest.ifr_mtu = mtu;
  151. if (ioctl(s, SIOCSIFMTU, &ifRequest) < 0) {
  152. int err = errno;
  153. close(s);
  154. Except_throw(eh, "ioctl(SIOCSIFMTU) [%s]", strerror(err));
  155. }
  156. close(s);
  157. }