multicast_device.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. device.c -- multicast socket
  3. Copyright (C) 2002-2005 Ivo Timmermans,
  4. 2002-2014 Guus Sliepen <guus@tinc-vpn.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include "conf.h"
  19. #include "device.h"
  20. #include "net.h"
  21. #include "logger.h"
  22. #include "netutl.h"
  23. #include "utils.h"
  24. #include "route.h"
  25. #include "xalloc.h"
  26. static const char *device_info = "multicast socket";
  27. static uint64_t device_total_in = 0;
  28. static uint64_t device_total_out = 0;
  29. static struct addrinfo *ai = NULL;
  30. static mac_t ignore_src = {{0}};
  31. static bool setup_device(void) {
  32. char *host;
  33. char *port;
  34. char *space;
  35. int ttl = 1;
  36. get_config_string(lookup_config(config_tree, "Interface"), &iface);
  37. if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
  38. logger(LOG_ERR, "Device variable required for %s", device_info);
  39. return false;
  40. }
  41. host = xstrdup(device);
  42. space = strchr(host, ' ');
  43. if(!space) {
  44. logger(LOG_ERR, "Port number required for %s", device_info);
  45. free(host);
  46. return false;
  47. }
  48. *space++ = 0;
  49. port = space;
  50. space = strchr(port, ' ');
  51. if(space) {
  52. *space++ = 0;
  53. ttl = atoi(space);
  54. }
  55. ai = str2addrinfo(host, port, SOCK_DGRAM);
  56. if(!ai) {
  57. free(host);
  58. return false;
  59. }
  60. device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
  61. if(device_fd < 0) {
  62. logger(LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
  63. free(host);
  64. return false;
  65. }
  66. #ifdef FD_CLOEXEC
  67. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  68. #endif
  69. static const int one = 1;
  70. setsockopt(device_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
  71. if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
  72. closesocket(device_fd);
  73. logger(LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
  74. free(host);
  75. return false;
  76. }
  77. switch(ai->ai_family) {
  78. #ifdef IP_ADD_MEMBERSHIP
  79. case AF_INET: {
  80. struct ip_mreq mreq;
  81. struct sockaddr_in in;
  82. memcpy(&in, ai->ai_addr, sizeof(in));
  83. mreq.imr_multiaddr.s_addr = in.sin_addr.s_addr;
  84. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  85. if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq))) {
  86. logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
  87. closesocket(device_fd);
  88. free(host);
  89. return false;
  90. }
  91. #ifdef IP_MULTICAST_LOOP
  92. setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&one, sizeof(one));
  93. #endif
  94. #ifdef IP_MULTICAST_TTL
  95. setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl));
  96. #endif
  97. }
  98. break;
  99. #endif
  100. #ifdef IPV6_JOIN_GROUP
  101. case AF_INET6: {
  102. struct ipv6_mreq mreq;
  103. struct sockaddr_in6 in6;
  104. memcpy(&in6, ai->ai_addr, sizeof(in6));
  105. memcpy(&mreq.ipv6mr_multiaddr, &in6.sin6_addr, sizeof(mreq.ipv6mr_multiaddr));
  106. mreq.ipv6mr_interface = in6.sin6_scope_id;
  107. if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&mreq, sizeof(mreq))) {
  108. logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
  109. closesocket(device_fd);
  110. free(host);
  111. return false;
  112. }
  113. #ifdef IPV6_MULTICAST_LOOP
  114. setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&one, sizeof(one));
  115. #endif
  116. #ifdef IPV6_MULTICAST_HOPS
  117. setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (void *)&ttl, sizeof(ttl));
  118. #endif
  119. }
  120. break;
  121. #endif
  122. default:
  123. logger(LOG_ERR, "Multicast for address family %x unsupported", ai->ai_family);
  124. closesocket(device_fd);
  125. free(host);
  126. return false;
  127. }
  128. free(host);
  129. logger(LOG_INFO, "%s is a %s", device, device_info);
  130. return true;
  131. }
  132. static void close_device(void) {
  133. close(device_fd);
  134. free(device);
  135. free(iface);
  136. if(ai) {
  137. freeaddrinfo(ai);
  138. }
  139. }
  140. static bool read_packet(vpn_packet_t *packet) {
  141. int lenin;
  142. if((lenin = recv(device_fd, (void *)packet->data, MTU, 0)) <= 0) {
  143. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
  144. device, strerror(errno));
  145. return false;
  146. }
  147. if(!memcmp(&ignore_src, packet->data + 6, sizeof(ignore_src))) {
  148. ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignoring loopback packet of %d bytes from %s", lenin, device_info);
  149. packet->len = 0;
  150. return true;
  151. }
  152. packet->len = lenin;
  153. device_total_in += packet->len;
  154. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
  155. device_info);
  156. return true;
  157. }
  158. static bool write_packet(vpn_packet_t *packet) {
  159. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
  160. packet->len, device_info);
  161. if(sendto(device_fd, (void *)packet->data, packet->len, 0, ai->ai_addr, ai->ai_addrlen) < 0) {
  162. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
  163. strerror(errno));
  164. return false;
  165. }
  166. device_total_out += packet->len;
  167. memcpy(&ignore_src, packet->data + 6, sizeof(ignore_src));
  168. return true;
  169. }
  170. static void dump_device_stats(void) {
  171. logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
  172. logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
  173. logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
  174. }
  175. const devops_t multicast_devops = {
  176. .setup = setup_device,
  177. .close = close_device,
  178. .read = read_packet,
  179. .write = write_packet,
  180. .dump_stats = dump_device_stats,
  181. };
  182. #if 0
  183. static bool not_supported(void) {
  184. logger(LOG_ERR, "Raw socket device not supported on this platform");
  185. return false;
  186. }
  187. const devops_t multicast_devops = {
  188. .setup = not_supported,
  189. .close = NULL,
  190. .read = NULL,
  191. .write = NULL,
  192. .dump_stats = NULL,
  193. };
  194. #endif