Mercurial > hg > quicktun
annotate src/common.c @ 16:8f564c559416
Exclude some local stuff
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sun, 27 Feb 2011 20:41:23 +0100 |
parents | 1fa5b5fa49e1 |
children | 38d495566d1c |
rev | line source |
---|---|
0 | 1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
2 Redistribution and use in source and binary forms, with or without modification, are | |
3 permitted provided that the following conditions are met: | |
4 | |
5 1. Redistributions of source code must retain the above copyright notice, this list of | |
6 conditions and the following disclaimer. | |
7 | |
8 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
9 of conditions and the following disclaimer in the documentation and/or other materials | |
10 provided with the distribution. | |
11 | |
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR | |
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
21 | |
22 The views and conclusions contained in the software and documentation are those of the | |
23 authors and should not be interpreted as representing official policies, either expressed | |
24 or implied, of Ivo Smits.*/ | |
25 | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include <fcntl.h> | |
30 #ifndef HAVE_NETINET_IN_H | |
31 #include <netinet/in.h> | |
32 #endif | |
33 #include <sys/ioctl.h> | |
7 | 34 #include <sys/socket.h> |
0 | 35 #include <poll.h> |
36 #include <netdb.h> | |
37 #include <stdlib.h> | |
8
6d86596d8884
Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents:
7
diff
changeset
|
38 #include <net/if.h> |
7 | 39 #ifdef linux |
40 #include <linux/if_tun.h> | |
41 #include <linux/if_ether.h> | |
42 #else | |
43 #define ETH_FRAME_LEN 1514 | |
44 #include <net/if_tun.h> | |
13 | 45 #ifdef SOLARIS |
46 #include <sys/stropts.h> | |
47 #include <sys/sockio.h> | |
48 #endif | |
7 | 49 #endif |
0 | 50 |
51 #define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information | |
52 | |
53 struct qtsession; | |
54 struct qtproto { | |
55 int encrypted; | |
56 int buffersize_raw; | |
57 int buffersize_enc; | |
58 int offset_raw; | |
59 int offset_enc; | |
60 int (*encode)(struct qtsession* sess, char* raw, char* enc, int len); | |
61 int (*decode)(struct qtsession* sess, char* enc, char* raw, int len); | |
62 int (*init)(struct qtsession* sess); | |
63 int protocol_data_size; | |
64 }; | |
65 struct qtsession { | |
66 struct qtproto protocol; | |
67 void* protocol_data; | |
68 int fd_socket; | |
69 int fd_dev; | |
70 int remote_float; | |
71 struct sockaddr_in remote_addr; | |
72 }; | |
73 | |
74 #ifdef COMBINED_BINARY | |
75 extern char* (*getconf)(const char*); | |
76 extern int errorexit(const char*); | |
77 extern int errorexitp(const char*); | |
78 extern void print_header(); | |
79 extern void hex2bin(unsigned char*, unsigned char*, int); | |
80 #else | |
81 | |
82 char* (*getconf)(const char*) = getenv; | |
83 | |
84 int errorexit(const char* text) { | |
85 fprintf(stderr, "%s\n", text); | |
86 return -1; | |
87 } | |
88 int errorexitp(const char* text) { | |
89 perror(text); | |
90 return -1; | |
91 } | |
92 | |
93 void print_header() { | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
94 fprintf(stderr, "UCIS QuickTun (c) 2010 Ivo Smits <Ivo@UCIS.nl>\n"); |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
95 fprintf(stderr, "More information: http://wiki.qontrol.nl/QuickTun\n"); |
0 | 96 } |
97 | |
98 int init_udp(struct qtsession* session) { | |
99 char* envval; | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
100 fprintf(stderr, "Initializing UDP socket...\n"); |
0 | 101 int sfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
102 if (sfd < 0) return errorexitp("Could not create UDP socket"); | |
103 struct sockaddr_in udpaddr; | |
104 struct hostent *he; | |
105 udpaddr.sin_family = AF_INET; | |
106 udpaddr.sin_addr.s_addr = INADDR_ANY; | |
107 udpaddr.sin_port = htons(2998); | |
108 if (envval = getconf("LOCAL_ADDRESS")) { | |
109 he = gethostbyname(envval); | |
110 if (!he) return errorexit("bind address lookup failed"); | |
111 else if (!he->h_addr_list[0]) return errorexit("no address to bind to"); | |
112 udpaddr.sin_addr.s_addr = *((unsigned long*)he->h_addr_list[0]); | |
113 udpaddr.sin_family = he->h_addrtype; | |
114 } | |
115 if (envval = getconf("LOCAL_PORT")) { | |
116 udpaddr.sin_port = htons(atoi(envval)); | |
117 } | |
118 if (bind(sfd, (struct sockaddr*)&udpaddr, sizeof(struct sockaddr_in))) return errorexitp("Could not bind socket"); | |
119 if (!(envval = getconf("REMOTE_ADDRESS"))) { | |
120 session->remote_float = 1; | |
121 //return errorexit("Missing REMOTE_ADDRESS"); | |
122 } else { | |
12
e4b60d041491
Make sure that the session buffer is zero
Ivo Smits <Ivo@UCIS.nl>
parents:
9
diff
changeset
|
123 session->remote_float = getconf("REMOTE_FLOAT") ? 1 : 0; |
0 | 124 he = gethostbyname(envval); |
125 if (!he) return errorexit("remote address lookup failed"); | |
126 else if (!he->h_addr_list[0]) return errorexit("no address to connect to"); | |
127 udpaddr.sin_family = he->h_addrtype; | |
128 udpaddr.sin_addr.s_addr = *((unsigned long*)he->h_addr_list[0]); | |
2
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
129 if (udpaddr.sin_addr.s_addr == 0) { |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
130 session->remote_float = 1; |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
131 } else { |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
132 if (envval = getconf("REMOTE_PORT")) { |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
133 udpaddr.sin_port = htons(atoi(envval)); |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
134 } |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
135 if (connect(sfd, (struct sockaddr*)&udpaddr, sizeof(struct sockaddr_in))) return errorexitp("Could not connect socket"); |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
136 session->remote_addr = udpaddr; |
0 | 137 } |
138 } | |
139 session->fd_socket = sfd; | |
140 return sfd; | |
141 } | |
142 | |
143 int init_tuntap() { | |
144 char* envval; | |
7 | 145 fprintf(stderr, "Initializing tun/tap device...\n"); |
0 | 146 int ttfd; //Tap device file descriptor |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
147 #ifdef linux |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
148 struct ifreq ifr; //required for tun/tap setup |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
149 memset(&ifr, 0, sizeof(ifr)); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
150 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tun/tap device file"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
151 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
152 ifr.ifr_flags = getconf("TUN_MODE") ? IFF_TUN : IFF_TAP; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
153 ifr.ifr_flags |= getconf("USE_PI") ? 0 : IFF_NO_PI; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
154 if (ioctl(ttfd, TUNSETIFF, (void *)&ifr) < 0) return errorexitp("TUNSETIFF ioctl failed"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
155 #else |
13 | 156 #ifdef SOLARIS |
157 int ip_fd = -1, if_fd = -1, ppa = 0; | |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
158 if ((ttfd = open("/dev/tun", O_RDWR)) < 0) return errorexitp("Could not open tun device file"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
159 if ((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) return errorexitp("Could not open /dev/ip"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
160 if ((envval = getconf("INTERFACE"))) { |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
161 while (*envval && !isdigit((int)*envval)) envval++; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
162 ppa = atoi(envval); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
163 } |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
164 if ((ppa = ioctl(ttfd, TUNNEWPPA, ppa)) < 0) return errorexitp("Could not assign new PPA"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
165 if ((if_fd = open("/dev/tun", O_RDWR, 0)) < 0) return errorexitp("Could not open tun device file again"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
166 if (ioctl(if_fd, I_PUSH, "ip") < 0) return errorexitp("Could not push IP module"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
167 if (ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) return errorexitp("Could not set PPA"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
168 if (ioctl(ip_fd, I_LINK, if_fd) < 0) return errorexitp("Could not link TUN device to IP"); |
7 | 169 #else |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
170 if (!(envval = getconf("INTERFACE"))) envval = "/dev/tun0"; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
171 if ((ttfd = open(envval, O_RDWR)) < 0) return errorexitp("Could not open tun device file"); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
172 #endif |
7 | 173 #endif |
0 | 174 return ttfd; |
175 } | |
176 | |
177 void hex2bin(unsigned char* dest, unsigned char* src, int count) { | |
178 int i; | |
179 for (i = 0; i < count; i++) { | |
180 if (*src >= '0' && *src <= '9') *dest = *src - '0'; | |
181 else if (*src >= 'a' && * src <='f') *dest = *src - 'a' + 10; | |
182 else if (*src >= 'A' && * src <='F') *dest = *src - 'A' + 10; | |
183 src++; *dest = *dest << 4; | |
184 if (*src >= '0' && *src <= '9') *dest += *src - '0'; | |
185 else if (*src >= 'a' && *src <= 'f') *dest += *src - 'a' + 10; | |
186 else if (*src >= 'A' && *src <= 'F') *dest += *src - 'A' + 10; | |
187 src++; dest++; | |
188 } | |
189 } | |
190 | |
191 int qtrun(struct qtproto* p) { | |
192 struct qtsession session; | |
193 session.protocol = *p; | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
194 |
0 | 195 init_udp(&session); |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
196 int sfd = session.fd_socket; |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
197 if (sfd == -1) return -1; |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
198 |
0 | 199 session.fd_dev = init_tuntap(); |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
200 int ttfd = session.fd_dev; |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
201 if (ttfd == -1) return -1; |
0 | 202 |
203 char protocol_data[p->protocol_data_size]; | |
12
e4b60d041491
Make sure that the session buffer is zero
Ivo Smits <Ivo@UCIS.nl>
parents:
9
diff
changeset
|
204 memset(protocol_data, 0, p->protocol_data_size); |
0 | 205 session.protocol_data = &protocol_data; |
206 if (p->init) p->init(&session); | |
207 | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
208 fprintf(stderr, "The tunnel is now operational!\n"); |
0 | 209 |
210 struct pollfd fds[2]; | |
211 fds[0].fd = ttfd; | |
212 fds[0].events = POLLIN; | |
213 fds[1].fd = sfd; | |
214 fds[1].events = POLLIN; | |
215 | |
216 struct sockaddr_in recvaddr; | |
217 | |
218 char buffer_raw_a[p->buffersize_raw]; | |
219 char buffer_enc_a[p->buffersize_enc]; | |
220 char* buffer_raw = buffer_raw_a; | |
221 char* buffer_enc = buffer_enc_a; | |
222 | |
223 while (1) { | |
224 int len = poll(fds, 2, -1); | |
225 if (len < 0) return errorexitp("poll error"); | |
226 else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) return errorexit("poll error on tap device"); | |
227 else if (fds[1].revents & (POLLHUP | POLLNVAL)) return errorexit("poll error on udp socket"); | |
228 if (fds[0].revents & POLLIN) { | |
9
640f620a55cf
Bugfixes for floating remote and debian upgrade
Ivo Smits <Ivo@UCIS.nl>
parents:
8
diff
changeset
|
229 len = read(ttfd, buffer_raw + p->offset_raw, p->buffersize_raw); |
0 | 230 if (session.remote_float == 0 || session.remote_float == 2) { |
231 len = p->encode(&session, buffer_raw, buffer_enc, len); | |
232 if (len < 0) return len; | |
233 if (session.remote_float == 0) { | |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
234 len = write(sfd, buffer_enc + p->offset_enc, len); |
0 | 235 } else { |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
236 len = sendto(sfd, buffer_enc + p->offset_enc, len, 0, (struct sockaddr*)&session.remote_addr, sizeof(session.remote_addr)); |
0 | 237 } |
238 } | |
239 } | |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
240 if (fds[1].revents & POLLERR) { |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
241 int out; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
242 len = sizeof(out); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
243 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &out, &len); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
244 fprintf(stderr, "Received error %d on udp socket\n", out); |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
245 } |
0 | 246 if (fds[1].revents & POLLIN) { |
247 socklen_t recvaddr_len = sizeof(recvaddr); | |
248 if (session.remote_float == 0) { | |
249 len = read(sfd, buffer_enc + p->offset_enc, p->buffersize_enc); | |
250 } else { | |
251 len = recvfrom(sfd, buffer_enc + p->offset_enc, p->buffersize_enc, 0, (struct sockaddr*)&recvaddr, &recvaddr_len); | |
252 } | |
253 if (len < 0) { | |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
254 long long out; |
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
255 len = sizeof(out); |
0 | 256 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &out, &len); |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
257 fprintf(stderr, "Received end of file on udp socket (error %d)\n", out); |
0 | 258 } else { |
259 len = p->decode(&session, buffer_enc, buffer_raw, len); | |
260 if (len != 0 && session.remote_float != 0 && (session.remote_addr.sin_addr.s_addr != recvaddr.sin_addr.s_addr || session.remote_addr.sin_port != recvaddr.sin_port)) { | |
15
1fa5b5fa49e1
Fixed a race condition caused by reception of ICMP errors
Ivo Smits <Ivo@UCIS.nl>
parents:
13
diff
changeset
|
261 fprintf(stderr, "Remote endpoint has changed to %08X:%d\n", recvaddr.sin_addr.s_addr, ntohs(recvaddr.sin_port)); |
0 | 262 session.remote_addr = recvaddr; |
263 session.remote_float = 2; | |
264 } | |
265 if (len < 0) return len; | |
266 write(ttfd, buffer_raw + p->offset_raw, len); | |
267 } | |
268 } | |
269 } | |
270 return 0; | |
271 } | |
272 #endif |