2 device.c -- UML network socket
3 Copyright (C) 2002-2005 Ivo Timmermans,
4 2002-2009 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 static int listen_fd = -1;
35 static int request_fd = -1;
36 static int data_fd = -1;
37 static int write_fd = -1;
41 static char *device_info;
43 extern char *identname;
44 extern volatile bool running;
46 static uint64_t device_total_in = 0;
47 static uint64_t device_total_out = 0;
49 enum request_type { REQ_NEW_CONTROL };
51 static struct request {
54 enum request_type type;
55 struct sockaddr_un sock;
58 static struct sockaddr_un data_sun;
60 bool setup_device(void) {
61 struct sockaddr_un listen_sun;
62 static const int one = 1;
70 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
71 xasprintf(&device, LOCALSTATEDIR "/run/%s.umlsocket", identname);
73 get_config_string(lookup_config(config_tree, "Interface"), &iface);
75 device_info = "UML network socket";
77 if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
78 logger(LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
83 setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
85 if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
86 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
91 if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
92 logger(LOG_ERR, "Could not open data %s: %s", device_info, strerror(errno));
97 setsockopt(data_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
99 if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0) {
100 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
107 gettimeofday(&tv, NULL);
108 name.usecs = tv.tv_usec;
109 data_sun.sun_family = AF_UNIX;
110 memcpy(&data_sun.sun_path, &name, sizeof name);
112 if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof data_sun) < 0) {
113 logger(LOG_ERR, "Could not bind data %s: %s", device_info, strerror(errno));
118 if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
119 logger(LOG_ERR, "Could not open %s: %s", device_info,
124 setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
126 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
127 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
131 listen_sun.sun_family = AF_UNIX;
132 strncpy(listen_sun.sun_path, device, sizeof listen_sun.sun_path);
133 if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof listen_sun) < 0) {
134 logger(LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
138 if(listen(listen_fd, 1) < 0) {
139 logger(LOG_ERR, "Could not listen on %s %s: %s", device_info, device, strerror(errno));
143 device_fd = listen_fd;
146 logger(LOG_INFO, "%s is a %s", device, device_info);
148 if(routing_mode == RMODE_ROUTER)
149 overwrite_mac = true;
154 void close_device(void) {
170 if(iface) free(iface);
173 bool read_packet(vpn_packet_t *packet) {
179 socklen_t salen = sizeof sa;
181 request_fd = accept(listen_fd, &sa, &salen);
183 logger(LOG_ERR, "Could not accept connection to %s %s: %s", device_info, device, strerror(errno));
187 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
188 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
195 device_fd = request_fd;
202 if((lenin = read(request_fd, &request, sizeof request)) != sizeof request) {
203 logger(LOG_ERR, "Error while reading request from %s %s: %s", device_info,
204 device, strerror(errno));
209 if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
210 logger(LOG_ERR, "Unknown magic %x, version %d, request type %d from %s %s",
211 request.magic, request.version, request.type, device_info, device);
216 if(connect(write_fd, &request.sock, sizeof request.sock) < 0) {
217 logger(LOG_ERR, "Could not bind write %s: %s", device_info, strerror(errno));
222 write(request_fd, &data_sun, sizeof data_sun);
225 logger(LOG_INFO, "Connection with UML established");
232 if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
233 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
234 device, strerror(errno));
241 device_total_in += packet->len;
243 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
250 logger(LOG_ERR, "Invalid value for state variable in " __FILE__);
255 bool write_packet(vpn_packet_t *packet) {
257 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Dropping packet of %d bytes to %s: not connected to UML yet",
258 packet->len, device_info);
262 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
263 packet->len, device_info);
265 if(write(write_fd, packet->data, packet->len) < 0) {
266 if(errno != EINTR && errno != EAGAIN) {
267 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
274 device_total_out += packet->len;
279 void dump_device_stats(void) {
280 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
281 logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
282 logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);