multicast_device.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 char *device_info;
  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. device_info = "multicast socket";
  37. get_config_string(lookup_config(config_tree, "Interface"), &iface);
  38. if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
  39. logger(LOG_ERR, "Device variable required for %s", device_info);
  40. return false;
  41. }
  42. host = xstrdup(device);
  43. space = strchr(host, ' ');
  44. if(!space) {
  45. logger(LOG_ERR, "Port number required for %s", device_info);
  46. free(host);
  47. return false;
  48. }
  49. *space++ = 0;
  50. port = space;
  51. space = strchr(port, ' ');
  52. if(space) {
  53. *space++ = 0;
  54. ttl = atoi(space);
  55. }
  56. ai = str2addrinfo(host, port, SOCK_DGRAM);
  57. if(!ai) {
  58. free(host);
  59. return false;
  60. }
  61. device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
  62. if(device_fd < 0) {
  63. logger(LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
  64. free(host);
  65. return false;
  66. }
  67. #ifdef FD_CLOEXEC
  68. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  69. #endif
  70. static const int one = 1;
  71. setsockopt(device_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
  72. if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
  73. closesocket(device_fd);
  74. logger(LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
  75. free(host);
  76. return false;
  77. }
  78. switch(ai->ai_family) {
  79. #ifdef IP_ADD_MEMBERSHIP
  80. case AF_INET: {
  81. struct ip_mreq mreq;
  82. struct sockaddr_in in;
  83. memcpy(&in, ai->ai_addr, sizeof(in));
  84. mreq.imr_multiaddr.s_addr = in.sin_addr.s_addr;
  85. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  86. if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq))) {
  87. logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
  88. closesocket(device_fd);
  89. free(host);
  90. return false;
  91. }
  92. #ifdef IP_MULTICAST_LOOP
  93. setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&one, sizeof(one));
  94. #endif
  95. #ifdef IP_MULTICAST_TTL
  96. setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl));
  97. #endif
  98. }
  99. break;
  100. #endif
  101. #ifdef IPV6_JOIN_GROUP
  102. case AF_INET6: {
  103. struct ipv6_mreq mreq;
  104. struct sockaddr_in6 in6;
  105. memcpy(&in6, ai->ai_addr, sizeof(in6));
  106. memcpy(&mreq.ipv6mr_multiaddr, &in6.sin6_addr, sizeof(mreq.ipv6mr_multiaddr));
  107. mreq.ipv6mr_interface = in6.sin6_scope_id;
  108. if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&mreq, sizeof(mreq))) {
  109. logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
  110. closesocket(device_fd);
  111. free(host);
  112. return false;
  113. }
  114. #ifdef IPV6_MULTICAST_LOOP
  115. setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&one, sizeof(one));
  116. #endif
  117. #ifdef IPV6_MULTICAST_HOPS
  118. setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (void *)&ttl, sizeof(ttl));
  119. #endif
  120. }
  121. break;
  122. #endif
  123. default:
  124. logger(LOG_ERR, "Multicast for address family %x unsupported", ai->ai_family);
  125. closesocket(device_fd);
  126. free(host);
  127. return false;
  128. }
  129. free(host);
  130. logger(LOG_INFO, "%s is a %s", device, device_info);
  131. return true;
  132. }
  133. static void close_device(void) {
  134. close(device_fd);
  135. free(device);
  136. free(iface);
  137. if(ai) {
  138. freeaddrinfo(ai);
  139. }
  140. }
  141. static bool read_packet(vpn_packet_t *packet) {
  142. int lenin;
  143. if((lenin = recv(device_fd, (void *)packet->data, MTU, 0)) <= 0) {
  144. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
  145. device, strerror(errno));
  146. return false;
  147. }
  148. if(!memcmp(&ignore_src, packet->data + 6, sizeof(ignore_src))) {
  149. ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignoring loopback packet of %d bytes from %s", lenin, device_info);
  150. packet->len = 0;
  151. return true;
  152. }
  153. packet->len = lenin;
  154. device_total_in += packet->len;
  155. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
  156. device_info);
  157. return true;
  158. }
  159. static bool write_packet(vpn_packet_t *packet) {
  160. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
  161. packet->len, device_info);
  162. if(sendto(device_fd, (void *)packet->data, packet->len, 0, ai->ai_addr, ai->ai_addrlen) < 0) {
  163. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
  164. strerror(errno));
  165. return false;
  166. }
  167. device_total_out += packet->len;
  168. memcpy(&ignore_src, packet->data + 6, sizeof(ignore_src));
  169. return true;
  170. }
  171. static void dump_device_stats(void) {
  172. logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
  173. logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
  174. logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
  175. }
  176. const devops_t multicast_devops = {
  177. .setup = setup_device,
  178. .close = close_device,
  179. .read = read_packet,
  180. .write = write_packet,
  181. .dump_stats = dump_device_stats,
  182. };
  183. #if 0
  184. static bool not_supported(void) {
  185. logger(LOG_ERR, "Raw socket device not supported on this platform");
  186. return false;
  187. }
  188. const devops_t multicast_devops = {
  189. .setup = not_supported,
  190. .close = NULL,
  191. .read = NULL,
  192. .write = NULL,
  193. .dump_stats = NULL,
  194. };
  195. #endif