NetPlatform_win32.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #define _WIN32_WINNT 0x0600
  16. #include "exception/WinEr.h"
  17. #include "util/platform/netdev/NetPlatform.h"
  18. #include "util/Bits.h"
  19. #include "util/platform/Sockaddr.h"
  20. #include <winsock2.h>
  21. #include <windows.h>
  22. #define NET_LUID misalligned_NET_LUID
  23. #define PNET_LUID misalligned_PNET_LUID
  24. #define IF_LUID misalligned_IF_LUID
  25. #define PIF_LUID misalligned_PIF_LUID
  26. #include <ifdef.h>
  27. #undef NET_LUID
  28. #undef PNET_LUID
  29. #undef IF_LUID
  30. #undef PIF_LUID
  31. // mingw-w64 incorrectly pragma pack's this to 1 byte.
  32. typedef union NET_LUID {
  33. ULONG64 Value;
  34. __C89_NAMELESS struct { /* bitfield with 64 bit types. */
  35. ULONG64 Reserved :24;
  36. ULONG64 NetLuidIndex :24;
  37. ULONG64 IfType :16;
  38. } Info;
  39. } NET_LUID, *PNET_LUID;
  40. Assert_compileTime(sizeof(NET_LUID) == 8);
  41. typedef NET_LUID IF_LUID, *PIF_LUID;
  42. #include <ws2ipdef.h>
  43. #include <naptypes.h>
  44. #include <ntddndis.h>
  45. #include <string.h>
  46. #include <ws2def.h>
  47. #include <iprtrmib.h>
  48. #include <ifdef.h>
  49. #include <iphlpapi.h>
  50. static Er_DEFUN(NET_LUID getLuid(const char* name, struct Allocator* alloc))
  51. {
  52. uint16_t ifName[IF_MAX_STRING_SIZE + 1] = {0};
  53. WinEr_check(alloc,
  54. (!MultiByteToWideChar(CP_ACP, 0, name, strlen(name), ifName, IF_MAX_STRING_SIZE + 1))
  55. );
  56. NET_LUID out;
  57. WinEr_check(alloc, ConvertInterfaceAliasToLuid(ifName, &out));
  58. Er_ret(out);
  59. }
  60. static LONG flushAddresses(NET_LUID luid, MIB_UNICASTIPADDRESS_TABLE* table)
  61. {
  62. LONG out = NO_ERROR;
  63. for (int i = 0; i < (int)table->NumEntries; i++) {
  64. if (table->Table[i].InterfaceLuid.Value == luid.Value) {
  65. if ((out = DeleteUnicastIpAddressEntry(&table->Table[i]))) {
  66. return out;
  67. }
  68. }
  69. }
  70. return out;
  71. }
  72. Er_DEFUN(void NetPlatform_flushAddresses(const char* deviceName, struct Allocator* alloc))
  73. {
  74. NET_LUID luid = Er(getLuid(deviceName, alloc));
  75. MIB_UNICASTIPADDRESS_TABLE* table;
  76. WinEr_check(alloc, GetUnicastIpAddressTable(AF_INET, &table));
  77. LONG ret = flushAddresses(luid, table);
  78. FreeMibTable(table);
  79. if (ret) {
  80. WinEr_fail(alloc, "DeleteUnicastIpAddressEntry(&table->Table[i])", ret);
  81. }
  82. WinEr_check(alloc, GetUnicastIpAddressTable(AF_INET6, &table));
  83. ret = flushAddresses(luid, table);
  84. FreeMibTable(table);
  85. if (ret) {
  86. WinEr_fail(alloc, "DeleteUnicastIpAddressEntry(&table->Table[i])", ret);
  87. }
  88. Er_ret();
  89. }
  90. #include "util/Hex.h"
  91. #include <stdio.h>
  92. static Er_DEFUN(void setupRoute(const char* deviceName,
  93. const uint8_t* addrBytes,
  94. int prefixLen,
  95. int addrFam,
  96. struct Allocator* alloc))
  97. {
  98. void WINAPI InitializeIpForwardEntry(PMIB_IPFORWARD_ROW2 Row);
  99. MIB_IPFORWARD_ROW2 row = {
  100. .InterfaceLuid = Er(getLuid(deviceName, alloc)),
  101. .ValidLifetime = WSA_INFINITE,
  102. .PreferredLifetime = WSA_INFINITE,
  103. .Metric = 0xffffffff,
  104. .Protocol = MIB_IPPROTO_NETMGMT,
  105. .SitePrefixLength = 255,
  106. .DestinationPrefix = {
  107. .PrefixLength = prefixLen,
  108. .Prefix = { .si_family = addrFam }
  109. },
  110. .NextHop = { .si_family = addrFam },
  111. .Loopback = false,
  112. .AutoconfigureAddress = false,
  113. .Immortal = false,
  114. .Age = 0,
  115. .Origin = 0
  116. };
  117. if (addrFam == AF_INET6) {
  118. Bits_memcpy(&row.DestinationPrefix.Prefix.Ipv6.sin6_addr, addrBytes, 15);
  119. row.DestinationPrefix.Prefix.Ipv6.sin6_family = AF_INET6;
  120. // set the gateway addr to the client's addr +1
  121. uint64_t addr[2];
  122. Bits_memcpy(addr, addrBytes, 16);
  123. addr[1] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[1]) + 1);
  124. if (!addr[1]) {
  125. addr[0] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[0]) + 1);
  126. }
  127. // Bits_memcpy(&row.NextHop.Ipv6.sin6_addr, addr, 16);
  128. } else {
  129. Bits_memcpy(&row.DestinationPrefix.Prefix.Ipv4.sin_addr, addrBytes, 4);
  130. row.DestinationPrefix.Prefix.Ipv4.sin_family = AF_INET;
  131. uint32_t addr;
  132. Bits_memcpy(&addr, addrBytes, 4);
  133. addr = Endian_hostToBigEndian32(Endian_bigEndianToHost32(addr) + 1);
  134. Bits_memcpy(&row.NextHop.Ipv4.sin_addr, &addr, 4);
  135. }
  136. //InitializeIpForwardEntry(&row);
  137. uint8_t buff[sizeof(row) * 2 + 1];
  138. Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  139. printf("%s %d\n", buff, row.SitePrefixLength);
  140. // Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  141. // printf("%s %d<\n", buff, row.SitePrefixLength);
  142. WinEr_check(alloc, CreateIpForwardEntry2(&row));
  143. Er_ret();
  144. }
  145. Er_DEFUN(void NetPlatform_addAddress(const char* interfaceName,
  146. const uint8_t* address,
  147. int prefixLen,
  148. int addrFam,
  149. struct Log* logger,
  150. struct Allocator* tempAlloc))
  151. {
  152. MIB_UNICASTIPADDRESS_ROW ipRow = {
  153. .PrefixOrigin = IpPrefixOriginUnchanged,
  154. .SuffixOrigin = IpSuffixOriginUnchanged,
  155. .ValidLifetime = 0xFFFFFFFF,
  156. .PreferredLifetime = 0xFFFFFFFF,
  157. .OnLinkPrefixLength = 0xFF
  158. };
  159. ipRow.InterfaceLuid = Er(getLuid(interfaceName, tempAlloc));
  160. ipRow.Address.si_family = addrFam;
  161. if (addrFam == Sockaddr_AF_INET6) {
  162. Bits_memcpy(&ipRow.Address.Ipv6.sin6_addr, address, 16);
  163. } else if (addrFam == Sockaddr_AF_INET) {
  164. Bits_memcpy(&ipRow.Address.Ipv4.sin_addr, address, 4);
  165. } else {
  166. Assert_true(0);
  167. }
  168. ipRow.OnLinkPrefixLength = prefixLen;
  169. WinEr_check(tempAlloc, CreateUnicastIpAddressEntry(&ipRow));
  170. if (0) {
  171. // setupRoute was disabled in d7f5b302ac9215221428de81b9a496b40dcb3884
  172. Er(setupRoute(interfaceName, address, prefixLen, addrFam, tempAlloc));
  173. }
  174. Er_ret();
  175. }
  176. Er_DEFUN(void NetPlatform_setMTU(const char* interfaceName,
  177. uint32_t mtu,
  178. struct Log* logger,
  179. struct Allocator* errAlloc))
  180. {
  181. // I looked all through the Windows API and setting the MTU is beyond me.
  182. // But I do know how to do it through netsh.
  183. // We know the connection names on Windows can't have any badly-behaved
  184. // characters that would need escaping.
  185. const char* format = ("netsh interface ipv6 set subinterface "
  186. "\"%s\" mtu=%d");
  187. // How much space do we need to fit the pattern substituted with the
  188. // interface name? We ought to use _vscprintf, but the compiler chokes on
  189. // its variable arguiment list. For now we overestimate: pattern length +
  190. // interrface name length + estimated size of the MTU number according to
  191. // <http://stackoverflow.com/a/3920025/402891> + a byte for the null
  192. // terminator.
  193. uint32_t totalSize = strlen(format) + strlen(interfaceName) +
  194. (CHAR_BIT * sizeof(uint32_t)) / 3 + 3 + 1;
  195. // Make a buffer to prepare our command in
  196. char buffer[totalSize];
  197. // Fill in the interface name
  198. snprintf(buffer, totalSize, format, interfaceName, mtu);
  199. Log_debug(logger, "Going to run command: %s", buffer);
  200. // Make the netsh call, and die if it returns the wrong thing.
  201. WinEr_check(errAlloc, system(buffer));
  202. // We should also configure the MTU for ipv4 (IpTunnel) case
  203. const char* format1 = ("netsh interface ipv4 set subinterface "
  204. "\"%s\" mtu=%d");
  205. snprintf(buffer, totalSize, format1, interfaceName, mtu);
  206. Log_debug(logger, "Going to run command: %s", buffer);
  207. WinEr_check(errAlloc, system(buffer));
  208. Er_ret();
  209. }
  210. Er_DEFUN(void NetPlatform_setRoutes(const char* ifName,
  211. struct Sockaddr** prefixSet,
  212. int prefixCount,
  213. struct Log* logger,
  214. struct Allocator* tempAlloc))
  215. {
  216. Er_raise(tempAlloc, "NetPlatform_setRoutes is not implemented in this platform.");
  217. }