NDPServer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "interface/tuntap/windows/NDPServer.h"
  16. #include "interface/InterfaceWrapper.h"
  17. #include "util/Bits.h"
  18. #include "util/Checksum.h"
  19. #include "util/Identity.h"
  20. #include "wire/Message.h"
  21. #include "wire/Ethernet.h"
  22. #include "wire/Headers.h"
  23. #include "wire/NDPHeader.h"
  24. struct NDPServer_pvt
  25. {
  26. struct NDPServer pub;
  27. struct Interface* wrapped;
  28. Identity
  29. };
  30. #define MULTICAST_ADDR "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\x00\x00\x08"
  31. // ff 02 00 00 00 00 00 00 00 00 00 01 ff 00 00 02 870099
  32. #define UNICAST_ADDR "\xfe\x80\0\0\0\0\0\0\0\0\0\0\0\0\0\x08"
  33. #define ALL_ROUTERS "\xff\x02\0\0\0\0\0\0\0\0\0\0\0\0\0\x02"
  34. #include <stdio.h>
  35. #include "util/Hex.h"
  36. static int sendResponse(struct Message* msg,
  37. struct Ethernet* eth,
  38. struct Headers_IP6Header* ip6,
  39. struct NDPServer_pvt* ns)
  40. {
  41. Bits_memcpyConst(ip6->destinationAddr, ip6->sourceAddr, 16);
  42. Bits_memcpyConst(ip6->sourceAddr, UNICAST_ADDR, 16);
  43. ip6->hopLimit = 255;
  44. struct NDPHeader_RouterAdvert* adv = (struct NDPHeader_RouterAdvert*) msg->bytes;
  45. adv->checksum = Checksum_icmp6(ip6->sourceAddr, msg->bytes, msg->length);
  46. Message_push(msg, ip6, sizeof(struct Headers_IP6Header), NULL);
  47. // Eth
  48. Message_push(msg, eth, sizeof(struct Ethernet), NULL);
  49. struct Ethernet* ethP = (struct Ethernet*) msg->bytes;
  50. Bits_memcpyConst(ethP->destAddr, eth->srcAddr, 6);
  51. Bits_memcpyConst(ethP->srcAddr, eth->destAddr, 6);
  52. printf("responding\n");
  53. Interface_sendMessage(ns->wrapped, msg);
  54. return 1;
  55. }
  56. static int tryRouterSolicitation(struct Message* msg,
  57. struct Ethernet* eth,
  58. struct Headers_IP6Header* ip6,
  59. struct NDPServer_pvt* ns)
  60. {
  61. if (msg->length < NDPHeader_RouterSolicitation_SIZE) {
  62. return 0;
  63. }
  64. struct NDPHeader_RouterSolicitation* sol = (struct NDPHeader_RouterSolicitation*)msg->bytes;
  65. if (sol->oneThirtyThree != 133 || sol->zero != 0) {
  66. printf("wrong type/code for router solicitation\n");
  67. return 0;
  68. }
  69. if (ns->pub.prefixLen < 1 || ns->pub.prefixLen > 128) {
  70. printf("address prefix not set\n");
  71. return 0;
  72. }
  73. if (Bits_memcmp(ip6->destinationAddr, UNICAST_ADDR, 16)
  74. && Bits_memcmp(ip6->destinationAddr, ALL_ROUTERS, 16))
  75. {
  76. printf("wrong address for router solicitation\n");
  77. return 0;
  78. }
  79. // now we're committed.
  80. Message_shift(msg, -msg->length, NULL);
  81. // Prefix option
  82. struct NDPHeader_RouterAdvert_PrefixOpt prefix = {
  83. .three = 3,
  84. .four = 4,
  85. .bits = 0,
  86. .validLifetimeSeconds_be = 0xffffffffu,
  87. .preferredLifetimeSeconds_be = 0xffffffffu,
  88. .reservedTwo = 0
  89. };
  90. Bits_memcpyConst(prefix.prefix, ns->pub.advertisePrefix, 16);
  91. prefix.prefixLen = ns->pub.prefixLen;
  92. Message_push(msg, &prefix, sizeof(struct NDPHeader_RouterAdvert_PrefixOpt), NULL);
  93. // NDP message
  94. struct NDPHeader_RouterAdvert adv = {
  95. .oneThirtyFour = 134,
  96. .zero = 0,
  97. .checksum = 0,
  98. .currentHopLimit = 0,
  99. .bits = 0,
  100. .routerLifetime_be = Endian_hostToBigEndian16(10),
  101. .reachableTime_be = 0,
  102. .retransTime_be = 0
  103. };
  104. Message_push(msg, &adv, sizeof(struct NDPHeader_RouterAdvert), NULL);
  105. sendResponse(msg, eth, ip6, ns);
  106. return 1;
  107. }
  108. static int tryNeighborSolicitation(struct Message* msg,
  109. struct Ethernet* eth,
  110. struct Headers_IP6Header* ip6,
  111. struct NDPServer_pvt* ns)
  112. {
  113. if (msg->length < NDPHeader_RouterSolicitation_SIZE) {
  114. return 0;
  115. }
  116. struct NDPHeader_NeighborSolicitation* sol = (struct NDPHeader_NeighborSolicitation*)msg->bytes;
  117. if (sol->oneThirtyFive != 135 || sol->zero != 0) {
  118. printf("wrong type/code for neighbor solicitation\n");
  119. return 0;
  120. }
  121. if (Bits_memcmp(ip6->destinationAddr, UNICAST_ADDR, 16)
  122. && Bits_memcmp(ip6->destinationAddr, MULTICAST_ADDR, 13))
  123. {
  124. printf("wrong address for neighbor solicitation\n");
  125. return 0;
  126. }
  127. // now we're committed.
  128. Message_shift(msg, -msg->length, NULL);
  129. struct NDPHeader_NeighborAdvert_MacOpt macOpt = {
  130. .two = 2,
  131. .one = 1
  132. };
  133. Bits_memcpyConst(macOpt.mac, eth->destAddr, 6);
  134. Message_push(msg, &macOpt, sizeof(struct NDPHeader_NeighborAdvert_MacOpt), NULL);
  135. struct NDPHeader_NeighborAdvert na = {
  136. .oneThirtySix = 136,
  137. .zero = 0,
  138. .checksum = 0,
  139. .bits = NDPHeader_NeighborAdvert_bits_ROUTER
  140. | NDPHeader_NeighborAdvert_bits_SOLICITED
  141. | NDPHeader_NeighborAdvert_bits_OVERRIDE
  142. };
  143. Bits_memcpyConst(na.targetAddr, UNICAST_ADDR, 16);
  144. Message_push(msg, &na, sizeof(struct NDPHeader_NeighborAdvert), NULL);
  145. sendResponse(msg, eth, ip6, ns);
  146. return 1;
  147. }
  148. static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
  149. {
  150. struct NDPServer_pvt* ns = Identity_check((struct NDPServer_pvt*)iface->receiverContext);
  151. if (msg->length < Ethernet_SIZE + Headers_IP6Header_SIZE) {
  152. return Interface_receiveMessage(&ns->pub.generic, msg);
  153. }
  154. struct Ethernet* eth = (struct Ethernet*) msg->bytes;
  155. struct Headers_IP6Header* ip6 = (struct Headers_IP6Header*) (&eth[1]);
  156. if (eth->ethertype != Ethernet_TYPE_IP6 || ip6->nextHeader != 58 /* ICMPv6 */) {
  157. return Interface_receiveMessage(&ns->pub.generic, msg);
  158. }
  159. // store the eth and ip6 headers so they don't get clobbered
  160. struct Ethernet storedEth;
  161. Message_pop(msg, &storedEth, sizeof(struct Ethernet), NULL);
  162. struct Headers_IP6Header storedIp6;
  163. Message_pop(msg, &storedIp6, sizeof(struct Headers_IP6Header), NULL);
  164. if (!tryNeighborSolicitation(msg, &storedEth, &storedIp6, ns)
  165. && !tryRouterSolicitation(msg, &storedEth, &storedIp6, ns))
  166. {
  167. Message_push(msg, &storedIp6, sizeof(struct Headers_IP6Header), NULL);
  168. Message_push(msg, &storedEth, sizeof(struct Ethernet), NULL);
  169. return Interface_receiveMessage(&ns->pub.generic, msg);
  170. }
  171. // responding happens in sendResponse..
  172. return 0;
  173. }
  174. static uint8_t sendMessage(struct Message* msg, struct Interface* iface)
  175. {
  176. struct NDPServer_pvt* ns = Identity_check((struct NDPServer_pvt*)iface);
  177. return Interface_sendMessage(ns->wrapped, msg);
  178. }
  179. struct NDPServer* NDPServer_new(struct Interface* external, struct Allocator* alloc)
  180. {
  181. struct NDPServer_pvt* out = Allocator_calloc(alloc, sizeof(struct NDPServer_pvt), 1);
  182. out->wrapped = external;
  183. Identity_set(out);
  184. InterfaceWrapper_wrap(external, sendMessage, receiveMessage, &out->pub.generic);
  185. return &out->pub;
  186. }