NetPlatform_win32.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_UTF8, 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_memcpyConst(&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_memcpyConst(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_memcpyConst(&row.NextHop.Ipv6.sin6_addr, addr, 16);
  127. } else {
  128. Bits_memcpyConst(&row.DestinationPrefix.Prefix.Ipv4.sin_addr, addrBytes, 4);
  129. row.DestinationPrefix.Prefix.Ipv4.sin_family = AF_INET;
  130. uint32_t addr;
  131. Bits_memcpyConst(&addr, addrBytes, 4);
  132. addr = Endian_hostToBigEndian32(Endian_bigEndianToHost32(addr) + 1);
  133. Bits_memcpyConst(&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* name,
  144. const uint8_t* addrBytes,
  145. int prefixLen,
  146. int addrFam,
  147. struct Log* logger,
  148. struct Except* eh)
  149. {
  150. MIB_UNICASTIPADDRESS_ROW ipRow = {
  151. .PrefixOrigin = IpPrefixOriginUnchanged,
  152. .SuffixOrigin = IpSuffixOriginUnchanged,
  153. .ValidLifetime = 0xFFFFFFFF,
  154. .PreferredLifetime = 0xFFFFFFFF,
  155. .OnLinkPrefixLength = 0xFF
  156. };
  157. ipRow.InterfaceLuid = getLuid(name, eh);
  158. ipRow.Address.si_family = addrFam;
  159. if (addrFam == Sockaddr_AF_INET6) {
  160. Bits_memcpyConst(&ipRow.Address.Ipv6.sin6_addr, addrBytes, 16);
  161. } else if (addrFam == Sockaddr_AF_INET) {
  162. Bits_memcpyConst(&ipRow.Address.Ipv4.sin_addr, addrBytes, 4);
  163. } else {
  164. Assert_true(0);
  165. }
  166. ipRow.OnLinkPrefixLength = prefixLen;
  167. WinFail_check(eh, CreateUnicastIpAddressEntry(&ipRow));
  168. return;
  169. setupRoute(name, addrBytes, prefixLen, addrFam, eh);
  170. }
  171. void NetPlatform_setMTU(const char* interfaceName,
  172. uint32_t mtu,
  173. struct Log* logger,
  174. struct Except* eh)
  175. {
  176. // I looked all through the Windows API and setting the MTU is beyond me.
  177. // But I do know how to do it through netsh.
  178. // We know the connection names on Windows can't have any badly-behaved
  179. // characters that would need escaping.
  180. const char* format = ("netsh interface ipv6 set subinterface "
  181. "\"%s\" mtu=%d");
  182. // How much space do we need to fit the pattern substituted with the
  183. // interface name? We ought to use _vscprintf, but the compiler chokes on
  184. // its variable arguiment list. For now we overestimate: pattern length +
  185. // interrface name length + estimated size of the MTU number according to
  186. // <http://stackoverflow.com/a/3920025/402891> + a byte for the null
  187. // terminator.
  188. uint32_t totalSize = strlen(format) + strlen(interfaceName) +
  189. (CHAR_BIT * sizeof(uint32_t)) / 3 + 3 + 1;
  190. // Make a buffer to prepare our command in
  191. char buffer[totalSize];
  192. // Fill in the interface name
  193. snprintf(buffer, totalSize, format, interfaceName, mtu);
  194. Log_debug(logger, "Going to run command: %s", buffer);
  195. // Make the netsh call, and die if it returns the wrong thing.
  196. WinFail_check(eh, system(buffer));
  197. }