uml_device.c 7.5 KB

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