-
Notifications
You must be signed in to change notification settings - Fork 63
/
tuntap-unix-openbsd.c
254 lines (216 loc) · 6.62 KB
/
tuntap-unix-openbsd.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
/*
* Copyright (c) 2012 Tristan Le Guern <tleguern@bouledef.eu>
*
* 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/param.h> /* For MAXPATHLEN */
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_tun.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet6/in6_var.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tuntap.h"
#include "private.h"
static int
tuntap_sys_create_dev(struct device *dev, const char *ifname, int tun) {
struct ifreq ifr;
/* At this point 'tun' can't be TUNTAP_ID_ANY */
(void)memset(&ifr, '\0', sizeof ifr);
(void)snprintf(ifr.ifr_name, IF_NAMESIZE, "%s%i", ifname, tun);
if (ioctl(dev->ctrl_sock, SIOCIFCREATE, &ifr) == -1) {
tuntap_log(TUNTAP_LOG_ERR, "Can't set persistent");
return -1;
}
return 0;
}
int
tuntap_sys_start(struct device *dev, int mode, int tun) {
int fd;
int persist;
char *ifname;
char name[MAXPATHLEN];
struct ifreq ifr;
/* Get the persistence bit */
if (mode & TUNTAP_MODE_PERSIST) {
mode &= ~TUNTAP_MODE_PERSIST;
persist = 1;
} else {
persist = 0;
}
/* Set the mode: tun or tap */
if (mode == TUNTAP_MODE_ETHERNET) {
ifname = "tap";
} else if (mode == TUNTAP_MODE_TUNNEL) {
ifname = "tun";
} else {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'mode'");
return -1;
}
/* And force the creation of the driver, if needed */
if (persist == 1 ) {
if (tuntap_sys_create_dev(dev, ifname, tun) == -1)
return -1;
}
/* Try to use the given driver or loop throught the avaible ones */
fd = -1;
if (tun < TUNTAP_ID_MAX) {
(void)snprintf(name, sizeof name, "/dev/%s%i", ifname, tun);
fd = open(name, O_RDWR);
} else if (tun == TUNTAP_ID_ANY) {
for (tun = 0; tun < TUNTAP_ID_MAX; ++tun) {
(void)memset(name, '\0', sizeof name);
(void)snprintf(name, sizeof name, "/dev/%s%i",
ifname, tun);
if ((fd = open(name, O_RDWR)) > 0)
break;
}
} else {
tuntap_log(TUNTAP_LOG_ERR, "Invalid parameter 'tun'");
return -1;
}
switch (fd) {
case -1:
tuntap_log(TUNTAP_LOG_ERR, "Permission denied");
return -1;
case 256:
tuntap_log(TUNTAP_LOG_ERR, "Can't find a tun entry");
return -1;
default:
/* NOTREACHED */
break;
}
/* Set the interface name */
(void)memset(&ifr, '\0', sizeof ifr);
(void)snprintf(ifr.ifr_name, sizeof ifr.ifr_name, "%s%i", ifname, tun);
/* And save it */
(void)strlcpy(dev->if_name, ifr.ifr_name, sizeof dev->if_name);
/* Get the interface default values */
if (ioctl(dev->ctrl_sock, SIOCGIFFLAGS, &ifr) == -1) {
tuntap_log(TUNTAP_LOG_ERR, "Can't get interface values");
return -1;
}
/* Set back our modifications */
if (ioctl(dev->ctrl_sock, SIOCSIFFLAGS, &ifr) == -1) {
tuntap_log(TUNTAP_LOG_ERR, "Can't set interface values");
return -1;
}
/* Save flags for tuntap_{up, down} */
dev->flags = ifr.ifr_flags;
/* Save pre-existing MAC address */
if (mode == TUNTAP_MODE_ETHERNET) {
struct ether_addr addr;
if (ioctl(fd, SIOCGIFADDR, &addr) == -1) {
tuntap_log(TUNTAP_LOG_WARN,
"Can't get link-layer address");
return fd;
}
(void)memcpy(dev->hwaddr, &addr, ETHER_ADDR_LEN);
}
return fd;
}
void
tuntap_sys_destroy(struct device *dev) {
struct ifreq ifr;
(void)memset(&ifr, '\0', sizeof ifr);
(void)strlcpy(ifr.ifr_name, dev->if_name, sizeof ifr.ifr_name);
if (ioctl(dev->ctrl_sock, SIOCIFDESTROY, &ifr) == -1)
tuntap_log(TUNTAP_LOG_WARN, "Can't destroy the interface");
}
int
tuntap_sys_set_hwaddr(struct device *dev, struct ether_addr *eth_addr) {
struct ifreq ifr;
(void)memset(&ifr, '\0', sizeof ifr);
(void)strlcpy(ifr.ifr_name, dev->if_name, sizeof ifr.ifr_name);
ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
ifr.ifr_addr.sa_family = AF_LINK;
(void)memcpy(ifr.ifr_addr.sa_data, eth_addr, ETHER_ADDR_LEN);
if (ioctl(dev->ctrl_sock, SIOCSIFLLADDR, (caddr_t)&ifr) < 0) {
tuntap_log(TUNTAP_LOG_ERR, "Can't set link-layer address");
return -1;
}
return 0;
}
int
tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s4, uint32_t bits) {
struct ifaliasreq ifa;
struct ifreq ifr;
struct sockaddr_in mask;
struct sockaddr_in addr;
(void)memset(&ifa, '\0', sizeof ifa);
(void)strlcpy(ifa.ifra_name, dev->if_name, sizeof ifa.ifra_name);
(void)memset(&ifr, '\0', sizeof ifr);
(void)strlcpy(ifr.ifr_name, dev->if_name, sizeof ifr.ifr_name);
/* Delete previously assigned address */
(void)ioctl(dev->ctrl_sock, SIOCDIFADDR, &ifr);
/*
* Fill-in the destination address and netmask,
* but don't care of the broadcast address
*/
(void)memset(&addr, '\0', sizeof addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = s4->s_addr;
addr.sin_len = sizeof addr;
(void)memcpy(&ifa.ifra_addr, &addr, sizeof addr);
(void)memset(&mask, '\0', sizeof mask);
mask.sin_family = AF_INET;
mask.sin_addr.s_addr = bits;
mask.sin_len = sizeof mask;
(void)memcpy(&ifa.ifra_mask, &mask, sizeof mask);
/* Simpler than calling SIOCSIFADDR and/or SIOCSIFBRDADDR */
if (ioctl(dev->ctrl_sock, SIOCAIFADDR, &ifa) == -1) {
tuntap_log(TUNTAP_LOG_ERR, "Can't set IP/netmask");
return -1;
}
return 0;
}
int
tuntap_sys_set_descr(struct device *dev, const char *descr, size_t len) {
struct ifreq ifr;
(void)len;
(void)memset(&ifr, '\0', sizeof ifr);
(void)strlcpy(ifr.ifr_name, dev->if_name, sizeof ifr.ifr_name);
ifr.ifr_data = (void *)descr;
if (ioctl(dev->ctrl_sock, SIOCSIFDESCR, &ifr) == -1) {
tuntap_log(TUNTAP_LOG_ERR,
"Can't set the interface description");
return -1;
}
return 0;
}
char *
tuntap_sys_get_descr(struct device *dev) {
struct ifreq ifr;
char buf[4096];
void *descr;
descr = buf;
(void)memset(&ifr, 0, sizeof ifr);
(void)strlcpy(ifr.ifr_name, dev->if_name, sizeof ifr.ifr_name);
ifr.ifr_data = descr;
if (ioctl(dev->ctrl_sock, SIOCGIFDESCR, &ifr) == -1) {
tuntap_log(TUNTAP_LOG_ERR,
"Can't get the interface description");
return NULL;
}
return (char *)descr;
}