NetPlatform_win32.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #define _WIN32_WINNT 0x0600
  16. #include "exception/WinFail.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 NET_LUID getLuid(const char* name, struct Except* eh)
  51. {
  52. uint16_t ifName[IF_MAX_STRING_SIZE + 1] = {0};
  53. WinFail_check(eh,
  54. (!MultiByteToWideChar(CP_ACP, 0, name, strlen(name), ifName, IF_MAX_STRING_SIZE + 1))
  55. );
  56. NET_LUID out;
  57. WinFail_check(eh, ConvertInterfaceAliasToLuid(ifName, &out));
  58. return 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. void NetPlatform_flushAddresses(const char* deviceName, struct Except* eh)
  73. {
  74. NET_LUID luid = getLuid(deviceName, eh);
  75. MIB_UNICASTIPADDRESS_TABLE* table;
  76. WinFail_check(eh, GetUnicastIpAddressTable(AF_INET, &table));
  77. LONG ret = flushAddresses(luid, table);
  78. FreeMibTable(table);
  79. if (ret) {
  80. WinFail_fail(eh, "DeleteUnicastIpAddressEntry(&table->Table[i])", ret);
  81. }
  82. WinFail_check(eh, GetUnicastIpAddressTable(AF_INET6, &table));
  83. ret = flushAddresses(luid, table);
  84. FreeMibTable(table);
  85. if (ret) {
  86. WinFail_fail(eh, "DeleteUnicastIpAddressEntry(&table->Table[i])", ret);
  87. }
  88. }
  89. #include "util/Hex.h"
  90. #include <stdio.h>
  91. static void setupRoute(const char* deviceName,
  92. const uint8_t* addrBytes,
  93. int prefixLen,
  94. int addrFam,
  95. struct Except* eh)
  96. {
  97. void WINAPI InitializeIpForwardEntry(PMIB_IPFORWARD_ROW2 Row);
  98. MIB_IPFORWARD_ROW2 row = {
  99. .InterfaceLuid = getLuid(deviceName, eh),
  100. .ValidLifetime = WSA_INFINITE,
  101. .PreferredLifetime = WSA_INFINITE,
  102. .Metric = 0xffffffff,
  103. .Protocol = MIB_IPPROTO_NETMGMT,
  104. .SitePrefixLength = 255,
  105. .DestinationPrefix = {
  106. .PrefixLength = prefixLen,
  107. .Prefix = { .si_family = addrFam }
  108. },
  109. .NextHop = { .si_family = addrFam },
  110. .Loopback = false,
  111. .AutoconfigureAddress = false,
  112. .Immortal = false,
  113. .Age = 0,
  114. .Origin = 0
  115. };
  116. if (addrFam == AF_INET6) {
  117. Bits_memcpy(&row.DestinationPrefix.Prefix.Ipv6.sin6_addr, addrBytes, 15);
  118. row.DestinationPrefix.Prefix.Ipv6.sin6_family = AF_INET6;
  119. // set the gateway addr to the client's addr +1
  120. uint64_t addr[2];
  121. Bits_memcpy(addr, addrBytes, 16);
  122. addr[1] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[1]) + 1);
  123. if (!addr[1]) {
  124. addr[0] = Endian_hostToBigEndian64(Endian_bigEndianToHost64(addr[0]) + 1);
  125. }
  126. // Bits_memcpy(&row.NextHop.Ipv6.sin6_addr, addr, 16);
  127. } else {
  128. Bits_memcpy(&row.DestinationPrefix.Prefix.Ipv4.sin_addr, addrBytes, 4);
  129. row.DestinationPrefix.Prefix.Ipv4.sin_family = AF_INET;
  130. uint32_t addr;
  131. Bits_memcpy(&addr, addrBytes, 4);
  132. addr = Endian_hostToBigEndian32(Endian_bigEndianToHost32(addr) + 1);
  133. Bits_memcpy(&row.NextHop.Ipv4.sin_addr, &addr, 4);
  134. }
  135. //InitializeIpForwardEntry(&row);
  136. uint8_t buff[sizeof(row) * 2 + 1];
  137. Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  138. printf("%s %d\n", buff, row.SitePrefixLength);
  139. // Hex_encode(buff, sizeof(buff), (uint8_t*) &row, sizeof(row));
  140. // printf("%s %d<\n", buff, row.SitePrefixLength);
  141. WinFail_check(eh, CreateIpForwardEntry2(&row));
  142. }
  143. void NetPlatform_addAddress(const char* interfaceName,
  144. const uint8_t* address,
  145. int prefixLen,
  146. int addrFam,
  147. struct Log* logger,
  148. struct Allocator* tempAlloc,
  149. struct Except* eh)
  150. {
  151. MIB_UNICASTIPADDRESS_ROW ipRow = {
  152. .PrefixOrigin = IpPrefixOriginUnchanged,
  153. .SuffixOrigin = IpSuffixOriginUnchanged,
  154. .ValidLifetime = 0xFFFFFFFF,
  155. .PreferredLifetime = 0xFFFFFFFF,
  156. .OnLinkPrefixLength = 0xFF
  157. };
  158. ipRow.InterfaceLuid = getLuid(name, eh);
  159. ipRow.Address.si_family = addrFam;
  160. if (addrFam == Sockaddr_AF_INET6) {
  161. Bits_memcpy(&ipRow.Address.Ipv6.sin6_addr, addrBytes, 16);
  162. } else if (addrFam == Sockaddr_AF_INET) {
  163. Bits_memcpy(&ipRow.Address.Ipv4.sin_addr, addrBytes, 4);
  164. } else {
  165. Assert_true(0);
  166. }
  167. ipRow.OnLinkPrefixLength = prefixLen;
  168. WinFail_check(eh, CreateUnicastIpAddressEntry(&ipRow));
  169. return;
  170. setupRoute(name, addrBytes, prefixLen, addrFam, eh);
  171. }
  172. void NetPlatform_setMTU(const char* interfaceName,
  173. uint32_t mtu,
  174. struct Log* logger,
  175. struct Except* eh)
  176. {
  177. // I looked all through the Windows API and setting the MTU is beyond me.
  178. // But I do know how to do it through netsh.
  179. // We know the connection names on Windows can't have any badly-behaved
  180. // characters that would need escaping.
  181. const char* format = ("netsh interface ipv6 set subinterface "
  182. "\"%s\" mtu=%d");
  183. // How much space do we need to fit the pattern substituted with the
  184. // interface name? We ought to use _vscprintf, but the compiler chokes on
  185. // its variable arguiment list. For now we overestimate: pattern length +
  186. // interrface name length + estimated size of the MTU number according to
  187. // <http://stackoverflow.com/a/3920025/402891> + a byte for the null
  188. // terminator.
  189. uint32_t totalSize = strlen(format) + strlen(interfaceName) +
  190. (CHAR_BIT * sizeof(uint32_t)) / 3 + 3 + 1;
  191. // Make a buffer to prepare our command in
  192. char buffer[totalSize];
  193. // Fill in the interface name
  194. snprintf(buffer, totalSize, format, interfaceName, mtu);
  195. Log_debug(logger, "Going to run command: %s", buffer);
  196. // Make the netsh call, and die if it returns the wrong thing.
  197. WinFail_check(eh, system(buffer));
  198. }
  199. void NetPlatform_setRoutes(const char* ifName,
  200. struct Sockaddr** prefixSet,
  201. int prefixCount,
  202. struct Log* logger,
  203. struct Allocator* tempAlloc,
  204. struct Except* eh)
  205. {
  206. Except_throw(eh, "NetPlatform_addRoute is not implemented in this platform.");
  207. }