forked from LaKabane/libtuntap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuntap-unix.c
355 lines (298 loc) · 7.29 KB
/
tuntap-unix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright (c) 2012 Tristan Le Guern <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if defined Linux
# include <netinet/ether.h>
# include <linux/if_tun.h>
#else
# include <net/if.h>
# if defined DragonFly
# include <net/tun/if_tun.h>
# elif !defined Darwin
# include <net/if_tun.h>
# endif
# include <netinet/in.h>
# include <netinet/if_ether.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "tuntap.h"
#include "private.h"
int
tuntap_start(struct device *dev, int mode, int tun) {
int sock;
int fd;
fd = sock = -1;
/* Don't re-initialise a previously started device */
if (dev->tun_fd != -1) {
tuntap_log(TUNTAP_LOG_ERR, "Device is already started");
return -1;
}
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
goto clean;
}
dev->ctrl_sock = sock;
if (mode & TUNTAP_MODE_PERSIST && tun == TUNTAP_ID_ANY) {
goto clean; /* XXX: Explain why */
}
fd = tuntap_sys_start(dev, mode, tun);
if (fd == -1) {
goto clean;
}
dev->tun_fd = fd;
tuntap_set_debug(dev, 0);
return 0;
clean:
if (fd != -1) {
(void)close(fd);
}
if (sock != -1) {
(void)close(sock);
}
return -1;
}
void
tuntap_release(struct device *dev) {
(void)close(dev->tun_fd);
(void)close(dev->ctrl_sock);
free(dev);
}
int
tuntap_set_descr(struct device *dev, const char *descr) {
size_t len;
if (descr == NULL) {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'descr'");
return -1;
}
len = strlen(descr);
if (len > IF_DESCRSIZE) {
/* The value will be troncated */
tuntap_log(TUNTAP_LOG_WARN, "Parameter 'descr' is too long");
}
if (tuntap_sys_set_descr(dev, descr, len) == -1) {
return -1;
}
return 0;
}
char *
tuntap_get_descr(struct device *dev) {
return tuntap_sys_get_descr(dev);
}
int
tuntap_set_ifname(struct device *dev, const char *ifname) {
size_t len;
if (ifname == NULL) {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'ifname'");
return -1;
}
len = strlen(ifname);
if (len > IF_NAMESIZE) {
tuntap_log(TUNTAP_LOG_ERR, "Parameter 'ifname' is too long");
return -1;
}
if (tuntap_sys_set_ifname(dev, ifname, len) == -1) {
return -1;
}
(void)memset(dev->if_name, 0, IF_NAMESIZE);
(void)strncpy(dev->if_name, ifname, len);
return 0;
}
char *
tuntap_get_hwaddr(struct device *dev) {
struct ether_addr eth_attr;
(void)memcpy(ð_attr, dev->hwaddr, sizeof dev->hwaddr);
return ether_ntoa(ð_attr);
}
int
tuntap_set_hwaddr(struct device *dev, const char *hwaddr) {
struct ether_addr *eth_addr, eth_rand;
if (strcmp(hwaddr, "random") == 0) {
unsigned int i;
unsigned char *ptr;
i = 0;
ptr = (unsigned char *)ð_rand;
srandom((unsigned int)time(NULL));
for (; i < sizeof eth_rand; ++i) {
*ptr = (unsigned char)random();
ptr++;
}
ptr = (unsigned char *)ð_rand;
*ptr &= 0xfc;
eth_addr = ð_rand;
} else {
eth_addr = ether_aton(hwaddr);
if (eth_addr == NULL) {
return -1;
}
}
(void)memcpy(dev->hwaddr, eth_addr, ETHER_ADDR_LEN);
if (tuntap_sys_set_hwaddr(dev, eth_addr) == -1)
return -1;
return 0;
}
int
tuntap_up(struct device *dev) {
struct ifreq ifr;
(void)memset(&ifr, '\0', sizeof ifr);
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
ifr.ifr_flags = (short int)dev->flags;
ifr.ifr_flags |= IFF_UP;
if (ioctl(dev->ctrl_sock, SIOCSIFFLAGS, &ifr) == -1) {
return -1;
}
dev->flags = ifr.ifr_flags;
return 0;
}
int
tuntap_down(struct device *dev) {
struct ifreq ifr;
(void)memset(&ifr, '\0', sizeof ifr);
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
ifr.ifr_flags = (short)dev->flags;
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(dev->ctrl_sock, SIOCSIFFLAGS, &ifr) == -1) {
return -1;
}
dev->flags = ifr.ifr_flags;
return 0;
}
int
tuntap_get_mtu(struct device *dev) {
struct ifreq ifr;
/* Only accept started device */
if (dev->tun_fd == -1) {
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
(void)memset(&ifr, '\0', sizeof ifr);
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
if (ioctl(dev->ctrl_sock, SIOCGIFMTU, &ifr) == -1) {
tuntap_log(TUNTAP_LOG_ERR, "Can't get MTU");
return -1;
}
return ifr.ifr_mtu;
}
int
tuntap_set_mtu(struct device *dev, int mtu) {
struct ifreq ifr;
/* Only accept started device */
if (dev->tun_fd == -1) {
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
(void)memset(&ifr, '\0', sizeof ifr);
(void)memcpy(ifr.ifr_name, dev->if_name, sizeof dev->if_name);
ifr.ifr_mtu = mtu;
if (ioctl(dev->ctrl_sock, SIOCSIFMTU, &ifr) == -1) {
tuntap_log(TUNTAP_LOG_ERR, "Can't set MTU");
return -1;
}
return 0;
}
int
tuntap_read(struct device *dev, void *buf, size_t size) {
int n;
/* Only accept started device */
if (dev->tun_fd == -1) {
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
n = read(dev->tun_fd, buf, size);
if (n == -1) {
tuntap_log(TUNTAP_LOG_WARN, "Can't to read from device");
return -1;
}
return n;
}
int
tuntap_write(struct device *dev, void *buf, size_t size) {
int n;
/* Only accept started device */
if (dev->tun_fd == -1) {
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
n = write(dev->tun_fd, buf, size);
if (n == -1) {
tuntap_log(TUNTAP_LOG_WARN, "Can't write to device");
return -1;
}
return n;
}
int
tuntap_get_readable(struct device *dev) {
int n;
n = 0;
if (ioctl(dev->tun_fd, FIONREAD, &n) == -1) {
tuntap_log(TUNTAP_LOG_INFO, "Your system does not support"
" FIONREAD, fallback to MTU");
return tuntap_get_mtu(dev);
}
return n;
}
int
tuntap_set_nonblocking(struct device *dev, int set) {
if (ioctl(dev->tun_fd, FIONBIO, &set) == -1) {
switch(set) {
case 0:
tuntap_log(TUNTAP_LOG_ERR, "Can't unset nonblocking");
break;
case 1:
tuntap_log(TUNTAP_LOG_ERR, "Can't set nonblocking");
break;
default:
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'set'");
}
return -1;
}
return 0;
}
int
tuntap_set_debug(struct device *dev, int set) {
/* Only accept started device */
if (dev->tun_fd == -1) {
tuntap_log(TUNTAP_LOG_NOTICE, "Device is not started");
return 0;
}
#if !defined Darwin
if (ioctl(dev->tun_fd, TUNSDEBUG, &set) == -1) {
switch(set) {
case 0:
tuntap_log(TUNTAP_LOG_WARN, "Can't unset debug");
break;
case 1:
tuntap_log(TUNTAP_LOG_WARN, "Can't set debug");
break;
default:
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'set'");
}
return -1;
}
return 0;
#else
tuntap_log(TUNTAP_LOG_NOTICE,
"Your system does not support tuntap_set_debug()");
return -1;
#endif
}