NetPlatform_win32.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 Err_DEFUN getLuid(NET_LUID* out, 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. WinEr_check(alloc, ConvertInterfaceAliasToLuid(ifName, &out));
  57. return NULL;
  58. }
  59. static LONG flushAddresses(NET_LUID luid, MIB_UNICASTIPADDRESS_TABLE* table)
  60. {
  61. LONG out = NO_ERROR;
  62. for (int i = 0; i < (int)table->NumEntries; i++) {
  63. if (table->Table[i].InterfaceLuid.Value == luid.Value) {
  64. if ((out = DeleteUnicastIpAddressEntry(&table->Table[i]))) {
  65. return out;
  66. }
  67. }
  68. }
  69. return out;
  70. }
  71. Err_DEFUN NetPlatform_flushAddresses(const char* deviceName, struct Allocator* alloc)
  72. {
  73. NET_LUID luid = -1;
  74. Err(getLuid(&luid, 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. return NULL;
  89. }
  90. #include "util/Hex.h"
  91. #include <stdio.h>
  92. static Err_DEFUN 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 = 0,
  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. Err(getLuid(&row.InterfaceLuid deviceName, alloc));
  118. if (addrFam == AF_INET6) {
  119. Bits_memcpy(&row.DestinationPrefix.Prefix.Ipv6.sin6_addr, addrBytes, 15);
  120. row.DestinationPrefix.Prefix.Ipv6.sin6_family = AF_INET6;
  121. // set the gateway addr to the client's addr +1
  122. uint64_t addr[2];
  123. Bits_memcpy(addr, addrBytes, 16);
  124. addr[1] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[1]) + 1);
  125. if (!addr[1]) {
  126. addr[0] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[0]) + 1);
  127. }
  128. // Bits_memcpy(&row.NextHop.Ipv6.sin6_addr, addr, 16);
  129. } else {
  130. Bits_memcpy(&row.DestinationPrefix.Prefix.Ipv4.sin_addr, addrBytes, 4);
  131. row.DestinationPrefix.Prefix.Ipv4.sin_family = AF_INET;
  132. uint32_t addr;
  133. Bits_memcpy(&addr, addrBytes, 4);
  134. addr = Endian_hostToBigEndian32(Endian_bigEndianToHost32(addr) + 1);
  135. Bits_memcpy(&row.NextHop.Ipv4.sin_addr, &addr, 4);
  136. }
  137. //InitializeIpForwardEntry(&row);
  138. uint8_t buff[sizeof(row) * 2 + 1];
  139. Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  140. printf("%s %d\n", buff, row.SitePrefixLength);
  141. // Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  142. // printf("%s %d<\n", buff, row.SitePrefixLength);
  143. WinEr_check(alloc, CreateIpForwardEntry2(&row));
  144. return NULL;
  145. }
  146. Err_DEFUN NetPlatform_addAddress(const char* interfaceName,
  147. const uint8_t* address,
  148. int prefixLen,
  149. int addrFam,
  150. struct Log* logger,
  151. struct Allocator* tempAlloc)
  152. {
  153. MIB_UNICASTIPADDRESS_ROW ipRow = {
  154. .PrefixOrigin = IpPrefixOriginUnchanged,
  155. .SuffixOrigin = IpSuffixOriginUnchanged,
  156. .ValidLifetime = 0xFFFFFFFF,
  157. .PreferredLifetime = 0xFFFFFFFF,
  158. .OnLinkPrefixLength = 0xFF
  159. };
  160. Err(getLuid(&ipRow.InterfaceLuid, interfaceName, tempAlloc));
  161. ipRow.Address.si_family = addrFam;
  162. if (addrFam == AF_INET6) {
  163. Bits_memcpy(&ipRow.Address.Ipv6.sin6_addr, address, 16);
  164. } else if (addrFam == AF_INET) {
  165. Bits_memcpy(&ipRow.Address.Ipv4.sin_addr, address, 4);
  166. } else {
  167. Assert_true(0);
  168. }
  169. ipRow.OnLinkPrefixLength = prefixLen;
  170. WinEr_check(tempAlloc, CreateUnicastIpAddressEntry(&ipRow));
  171. if (0) {
  172. // setupRoute was disabled in d7f5b302ac9215221428de81b9a496b40dcb3884
  173. Err(setupRoute(interfaceName, address, prefixLen, addrFam, tempAlloc));
  174. }
  175. return NULL;
  176. }
  177. Err_DEFUN NetPlatform_setMTU(const char* interfaceName,
  178. uint32_t mtu,
  179. struct Log* logger,
  180. struct Allocator* errAlloc)
  181. {
  182. // I looked all through the Windows API and setting the MTU is beyond me.
  183. // But I do know how to do it through netsh.
  184. // We know the connection names on Windows can't have any badly-behaved
  185. // characters that would need escaping.
  186. const char* format = ("netsh interface ipv6 set subinterface "
  187. "\"%s\" mtu=%d");
  188. // How much space do we need to fit the pattern substituted with the
  189. // interface name? We ought to use _vscprintf, but the compiler chokes on
  190. // its variable arguiment list. For now we overestimate: pattern length +
  191. // interrface name length + estimated size of the MTU number according to
  192. // <http://stackoverflow.com/a/3920025/402891> + a byte for the null
  193. // terminator.
  194. uint32_t totalSize = strlen(format) + strlen(interfaceName) +
  195. (CHAR_BIT * sizeof(uint32_t)) / 3 + 3 + 1;
  196. // Make a buffer to prepare our command in
  197. char buffer[totalSize];
  198. // Fill in the interface name
  199. snprintf(buffer, totalSize, format, interfaceName, mtu);
  200. Log_debug(logger, "Going to run command: %s", buffer);
  201. // Make the netsh call, and die if it returns the wrong thing.
  202. WinEr_check(errAlloc, system(buffer));
  203. // We should also configure the MTU for ipv4 (IpTunnel) case
  204. const char* format1 = ("netsh interface ipv4 set subinterface "
  205. "\"%s\" mtu=%d");
  206. snprintf(buffer, totalSize, format1, interfaceName, mtu);
  207. Log_debug(logger, "Going to run command: %s", buffer);
  208. WinEr_check(errAlloc, system(buffer));
  209. return NULL;
  210. }
  211. Err_DEFUN NetPlatform_setRoutes(const char* ifName,
  212. struct Sockaddr** prefixSet,
  213. int prefixCount,
  214. struct Log* logger,
  215. struct Allocator* tempAlloc)
  216. {
  217. Err_raise(tempAlloc, "NetPlatform_setRoutes is not implemented in this platform.");
  218. }