uml_device.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. device.c -- UML network socket
  3. Copyright (C) 2002-2005 Ivo Timmermans,
  4. 2002-2012 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 <sys/un.h>
  19. #include "conf.h"
  20. #include "device.h"
  21. #include "net.h"
  22. #include "logger.h"
  23. #include "utils.h"
  24. #include "route.h"
  25. #include "xalloc.h"
  26. static int listen_fd = -1;
  27. static int request_fd = -1;
  28. static int data_fd = -1;
  29. static int write_fd = -1;
  30. static int state = 0;
  31. static char *device_info;
  32. extern char *identname;
  33. extern volatile bool running;
  34. static uint64_t device_total_in = 0;
  35. static uint64_t device_total_out = 0;
  36. enum request_type { REQ_NEW_CONTROL };
  37. static struct request {
  38. uint32_t magic;
  39. uint32_t version;
  40. enum request_type type;
  41. struct sockaddr_un sock;
  42. } request;
  43. static struct sockaddr_un data_sun;
  44. static bool setup_device(void) {
  45. struct sockaddr_un listen_sun;
  46. static const int one = 1;
  47. struct {
  48. char zero;
  49. int pid;
  50. int usecs;
  51. } name;
  52. struct timeval tv;
  53. if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
  54. xasprintf(&device, LOCALSTATEDIR "/run/%s.umlsocket", identname);
  55. }
  56. get_config_string(lookup_config(config_tree, "Interface"), &iface);
  57. device_info = "UML network socket";
  58. if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
  59. logger(LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
  60. running = false;
  61. return false;
  62. }
  63. #ifdef FD_CLOEXEC
  64. fcntl(write_fd, F_SETFD, FD_CLOEXEC);
  65. #endif
  66. setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  67. if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
  68. logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
  69. running = false;
  70. return false;
  71. }
  72. if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
  73. logger(LOG_ERR, "Could not open data %s: %s", device_info, strerror(errno));
  74. running = false;
  75. return false;
  76. }
  77. #ifdef FD_CLOEXEC
  78. fcntl(data_fd, F_SETFD, FD_CLOEXEC);
  79. #endif
  80. setsockopt(data_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  81. if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0) {
  82. logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
  83. running = false;
  84. return false;
  85. }
  86. name.zero = 0;
  87. name.pid = getpid();
  88. gettimeofday(&tv, NULL);
  89. name.usecs = tv.tv_usec;
  90. data_sun.sun_family = AF_UNIX;
  91. memcpy(&data_sun.sun_path, &name, sizeof(name));
  92. if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof(data_sun)) < 0) {
  93. logger(LOG_ERR, "Could not bind data %s: %s", device_info, strerror(errno));
  94. running = false;
  95. return false;
  96. }
  97. if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
  98. logger(LOG_ERR, "Could not open %s: %s", device_info,
  99. strerror(errno));
  100. return false;
  101. }
  102. #ifdef FD_CLOEXEC
  103. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  104. #endif
  105. setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  106. if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
  107. logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
  108. return false;
  109. }
  110. listen_sun.sun_family = AF_UNIX;
  111. strncpy(listen_sun.sun_path, device, sizeof(listen_sun.sun_path));
  112. if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof(listen_sun)) < 0) {
  113. logger(LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
  114. return false;
  115. }
  116. if(listen(listen_fd, 1) < 0) {
  117. logger(LOG_ERR, "Could not listen on %s %s: %s", device_info, device, strerror(errno));
  118. return false;
  119. }
  120. device_fd = listen_fd;
  121. state = 0;
  122. logger(LOG_INFO, "%s is a %s", device, device_info);
  123. if(routing_mode == RMODE_ROUTER) {
  124. overwrite_mac = true;
  125. }
  126. return true;
  127. }
  128. void close_device(void) {
  129. if(listen_fd >= 0) {
  130. close(listen_fd);
  131. }
  132. if(request_fd >= 0) {
  133. close(request_fd);
  134. }
  135. if(data_fd >= 0) {
  136. close(data_fd);
  137. }
  138. if(write_fd >= 0) {
  139. close(write_fd);
  140. }
  141. unlink(device);
  142. free(device);
  143. if(iface) {
  144. free(iface);
  145. }
  146. }
  147. static bool read_packet(vpn_packet_t *packet) {
  148. int lenin;
  149. switch(state) {
  150. case 0: {
  151. struct sockaddr sa;
  152. socklen_t salen = sizeof(sa);
  153. request_fd = accept(listen_fd, &sa, &salen);
  154. if(request_fd < 0) {
  155. logger(LOG_ERR, "Could not accept connection to %s %s: %s", device_info, device, strerror(errno));
  156. return false;
  157. }
  158. #ifdef FD_CLOEXEC
  159. fcntl(request_fd, F_SETFD, FD_CLOEXEC);
  160. #endif
  161. if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
  162. logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
  163. running = false;
  164. return false;
  165. }
  166. close(listen_fd);
  167. listen_fd = -1;
  168. device_fd = request_fd;
  169. state = 1;
  170. return false;
  171. }
  172. case 1: {
  173. if((lenin = read(request_fd, &request, sizeof(request))) != sizeof request) {
  174. logger(LOG_ERR, "Error while reading request from %s %s: %s", device_info,
  175. device, strerror(errno));
  176. running = false;
  177. return false;
  178. }
  179. if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
  180. logger(LOG_ERR, "Unknown magic %x, version %d, request type %d from %s %s",
  181. request.magic, request.version, request.type, device_info, device);
  182. running = false;
  183. return false;
  184. }
  185. if(connect(write_fd, &request.sock, sizeof(request.sock)) < 0) {
  186. logger(LOG_ERR, "Could not bind write %s: %s", device_info, strerror(errno));
  187. running = false;
  188. return false;
  189. }
  190. write(request_fd, &data_sun, sizeof(data_sun));
  191. device_fd = data_fd;
  192. logger(LOG_INFO, "Connection with UML established");
  193. state = 2;
  194. return false;
  195. }
  196. case 2: {
  197. if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
  198. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
  199. device, strerror(errno));
  200. running = false;
  201. return false;
  202. }
  203. packet->len = lenin;
  204. device_total_in += packet->len;
  205. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
  206. device_info);
  207. return true;
  208. }
  209. default:
  210. logger(LOG_ERR, "Invalid value for state variable in " FILE);
  211. abort();
  212. }
  213. }
  214. static bool write_packet(vpn_packet_t *packet) {
  215. if(state != 2) {
  216. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Dropping packet of %d bytes to %s: not connected to UML yet",
  217. packet->len, device_info);
  218. return false;
  219. }
  220. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
  221. packet->len, device_info);
  222. if(write(write_fd, packet->data, packet->len) < 0) {
  223. if(errno != EINTR && errno != EAGAIN) {
  224. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
  225. running = false;
  226. }
  227. return false;
  228. }
  229. device_total_out += packet->len;
  230. return true;
  231. }
  232. static void dump_device_stats(void) {
  233. logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
  234. logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
  235. logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
  236. }
  237. const devops_t uml_devops = {
  238. .setup = setup_device,
  239. .close = close_device,
  240. .read = read_packet,
  241. .write = write_packet,
  242. .dump_stats = dump_device_stats,
  243. };