tun.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2011, 2012 Christian Grothoff
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file tun/tun.c
  18. * @brief standard IP calculations for TUN interaction
  19. * @author Philipp Toelke
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. /**
  25. * IP TTL we use for packets that we assemble (8 bit unsigned integer)
  26. */
  27. #define FRESH_TTL 64
  28. /**
  29. * Initialize an IPv4 header.
  30. *
  31. * @param ip header to initialize
  32. * @param protocol protocol to use (i.e. IPPROTO_UDP)
  33. * @param payload_length number of bytes of payload that follow (excluding IPv4 header)
  34. * @param src source IP address to use
  35. * @param dst destination IP address to use
  36. */
  37. void
  38. GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip,
  39. uint8_t protocol,
  40. uint16_t payload_length,
  41. const struct in_addr *src,
  42. const struct in_addr *dst)
  43. {
  44. GNUNET_assert (20 == sizeof(struct GNUNET_TUN_IPv4Header));
  45. GNUNET_assert (payload_length <=
  46. UINT16_MAX - sizeof(struct GNUNET_TUN_IPv4Header));
  47. memset (ip, 0, sizeof(struct GNUNET_TUN_IPv4Header));
  48. ip->header_length = sizeof(struct GNUNET_TUN_IPv4Header) / 4;
  49. ip->version = 4;
  50. ip->total_length =
  51. htons (sizeof(struct GNUNET_TUN_IPv4Header) + payload_length);
  52. ip->identification =
  53. (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 65536);
  54. ip->ttl = FRESH_TTL;
  55. ip->protocol = protocol;
  56. ip->source_address = *src;
  57. ip->destination_address = *dst;
  58. ip->checksum =
  59. GNUNET_CRYPTO_crc16_n (ip, sizeof(struct GNUNET_TUN_IPv4Header));
  60. }
  61. /**
  62. * Initialize an IPv6 header.
  63. *
  64. * @param ip header to initialize
  65. * @param protocol protocol to use (i.e. IPPROTO_UDP), technically "next_header" for IPv6
  66. * @param payload_length number of bytes of payload that follow (excluding IPv6 header)
  67. * @param src source IP address to use
  68. * @param dst destination IP address to use
  69. */
  70. void
  71. GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip,
  72. uint8_t protocol,
  73. uint16_t payload_length,
  74. const struct in6_addr *src,
  75. const struct in6_addr *dst)
  76. {
  77. GNUNET_assert (40 == sizeof(struct GNUNET_TUN_IPv6Header));
  78. GNUNET_assert (payload_length <=
  79. UINT16_MAX - sizeof(struct GNUNET_TUN_IPv6Header));
  80. memset (ip, 0, sizeof(struct GNUNET_TUN_IPv6Header));
  81. ip->version = 6;
  82. ip->next_header = protocol;
  83. ip->payload_length = htons ((uint16_t) payload_length);
  84. ip->hop_limit = FRESH_TTL;
  85. ip->destination_address = *dst;
  86. ip->source_address = *src;
  87. }
  88. /**
  89. * Calculate IPv4 TCP checksum.
  90. *
  91. * @param ip ipv4 header fully initialized
  92. * @param tcp TCP header (initialized except for CRC)
  93. * @param payload the TCP payload
  94. * @param payload_length number of bytes of TCP payload
  95. */
  96. void
  97. GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
  98. struct GNUNET_TUN_TcpHeader *tcp,
  99. const void *payload,
  100. uint16_t payload_length)
  101. {
  102. uint32_t sum;
  103. uint16_t tmp;
  104. GNUNET_assert (20 == sizeof(struct GNUNET_TUN_TcpHeader));
  105. GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_IPv4Header)
  106. + sizeof(struct GNUNET_TUN_TcpHeader) ==
  107. ntohs (ip->total_length));
  108. GNUNET_assert (IPPROTO_TCP == ip->protocol);
  109. tcp->crc = 0;
  110. sum = GNUNET_CRYPTO_crc16_step (0,
  111. &ip->source_address,
  112. sizeof(struct in_addr) * 2);
  113. tmp = htons (IPPROTO_TCP);
  114. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
  115. tmp = htons (payload_length + sizeof(struct GNUNET_TUN_TcpHeader));
  116. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
  117. sum =
  118. GNUNET_CRYPTO_crc16_step (sum, tcp, sizeof(struct GNUNET_TUN_TcpHeader));
  119. sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
  120. tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
  121. }
  122. /**
  123. * Calculate IPv6 TCP checksum.
  124. *
  125. * @param ip ipv6 header fully initialized
  126. * @param tcp header (initialized except for CRC)
  127. * @param payload the TCP payload
  128. * @param payload_length number of bytes of TCP payload
  129. */
  130. void
  131. GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
  132. struct GNUNET_TUN_TcpHeader *tcp,
  133. const void *payload,
  134. uint16_t payload_length)
  135. {
  136. uint32_t sum;
  137. uint32_t tmp;
  138. GNUNET_assert (20 == sizeof(struct GNUNET_TUN_TcpHeader));
  139. GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_TcpHeader) ==
  140. ntohs (ip->payload_length));
  141. GNUNET_assert (IPPROTO_TCP == ip->next_header);
  142. tcp->crc = 0;
  143. sum = GNUNET_CRYPTO_crc16_step (0,
  144. &ip->source_address,
  145. 2 * sizeof(struct in6_addr));
  146. tmp = htonl (sizeof(struct GNUNET_TUN_TcpHeader) + payload_length);
  147. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
  148. tmp = htonl (IPPROTO_TCP);
  149. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
  150. sum =
  151. GNUNET_CRYPTO_crc16_step (sum, tcp, sizeof(struct GNUNET_TUN_TcpHeader));
  152. sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
  153. tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
  154. }
  155. /**
  156. * Calculate IPv4 UDP checksum.
  157. *
  158. * @param ip ipv4 header fully initialized
  159. * @param udp UDP header (initialized except for CRC)
  160. * @param payload the UDP payload
  161. * @param payload_length number of bytes of UDP payload
  162. */
  163. void
  164. GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
  165. struct GNUNET_TUN_UdpHeader *udp,
  166. const void *payload,
  167. uint16_t payload_length)
  168. {
  169. uint32_t sum;
  170. uint16_t tmp;
  171. GNUNET_assert (8 == sizeof(struct GNUNET_TUN_UdpHeader));
  172. GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_IPv4Header)
  173. + sizeof(struct GNUNET_TUN_UdpHeader) ==
  174. ntohs (ip->total_length));
  175. GNUNET_assert (IPPROTO_UDP == ip->protocol);
  176. udp->crc =
  177. 0; /* technically optional, but we calculate it anyway, just to be sure */
  178. sum = GNUNET_CRYPTO_crc16_step (0,
  179. &ip->source_address,
  180. sizeof(struct in_addr) * 2);
  181. tmp = htons (IPPROTO_UDP);
  182. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
  183. tmp = htons (sizeof(struct GNUNET_TUN_UdpHeader) + payload_length);
  184. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint16_t));
  185. sum =
  186. GNUNET_CRYPTO_crc16_step (sum, udp, sizeof(struct GNUNET_TUN_UdpHeader));
  187. sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
  188. udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
  189. }
  190. /**
  191. * Calculate IPv6 UDP checksum.
  192. *
  193. * @param ip ipv6 header fully initialized
  194. * @param udp UDP header (initialized except for CRC)
  195. * @param payload the UDP payload
  196. * @param payload_length number of bytes of UDP payload
  197. */
  198. void
  199. GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
  200. struct GNUNET_TUN_UdpHeader *udp,
  201. const void *payload,
  202. uint16_t payload_length)
  203. {
  204. uint32_t sum;
  205. uint32_t tmp;
  206. GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_UdpHeader) ==
  207. ntohs (ip->payload_length));
  208. GNUNET_assert (payload_length + sizeof(struct GNUNET_TUN_UdpHeader) ==
  209. ntohs (udp->len));
  210. GNUNET_assert (IPPROTO_UDP == ip->next_header);
  211. udp->crc = 0;
  212. sum = GNUNET_CRYPTO_crc16_step (0,
  213. &ip->source_address,
  214. sizeof(struct in6_addr) * 2);
  215. tmp = htons (sizeof(struct GNUNET_TUN_UdpHeader)
  216. + payload_length); /* aka udp->len */
  217. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
  218. tmp = htons (ip->next_header);
  219. sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof(uint32_t));
  220. sum =
  221. GNUNET_CRYPTO_crc16_step (sum, udp, sizeof(struct GNUNET_TUN_UdpHeader));
  222. sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
  223. udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
  224. }
  225. /**
  226. * Calculate ICMP checksum.
  227. *
  228. * @param icmp IMCP header (initialized except for CRC)
  229. * @param payload the ICMP payload
  230. * @param payload_length number of bytes of ICMP payload
  231. */
  232. void
  233. GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp,
  234. const void *payload,
  235. uint16_t payload_length)
  236. {
  237. uint32_t sum;
  238. GNUNET_assert (8 == sizeof(struct GNUNET_TUN_IcmpHeader));
  239. icmp->crc = 0;
  240. sum =
  241. GNUNET_CRYPTO_crc16_step (0, icmp, sizeof(struct GNUNET_TUN_IcmpHeader));
  242. sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
  243. icmp->crc = GNUNET_CRYPTO_crc16_finish (sum);
  244. }
  245. /**
  246. * Check if two sockaddrs are equal.
  247. *
  248. * @param sa one address
  249. * @param sb another address
  250. * @param include_port also check ports
  251. * @return #GNUNET_YES if they are equal
  252. */
  253. int
  254. GNUNET_TUN_sockaddr_cmp (const struct sockaddr *sa,
  255. const struct sockaddr *sb,
  256. int include_port)
  257. {
  258. if (sa->sa_family != sb->sa_family)
  259. return GNUNET_NO;
  260. switch (sa->sa_family)
  261. {
  262. case AF_INET: {
  263. const struct sockaddr_in *sa4 = (const struct sockaddr_in *) sa;
  264. const struct sockaddr_in *sb4 = (const struct sockaddr_in *) sb;
  265. if ((include_port) && (sa4->sin_port != sb4->sin_port))
  266. return GNUNET_NO;
  267. return(sa4->sin_addr.s_addr == sb4->sin_addr.s_addr);
  268. }
  269. case AF_INET6: {
  270. const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *) sa;
  271. const struct sockaddr_in6 *sb6 = (const struct sockaddr_in6 *) sb;
  272. if ((include_port) && (sa6->sin6_port != sb6->sin6_port))
  273. return GNUNET_NO;
  274. return(
  275. 0 == memcmp (&sa6->sin6_addr, &sb6->sin6_addr, sizeof(struct
  276. in6_addr)));
  277. }
  278. default:
  279. GNUNET_break (0);
  280. return GNUNET_SYSERR;
  281. }
  282. }
  283. /* end of tun.c */