-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuntap_linux.c
276 lines (221 loc) · 8.16 KB
/
tuntap_linux.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
/**
* (C) 2007-18 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/>
*
*/
#include "n2n.h"
#ifdef __linux__
#include <net/if_arp.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
/* ********************************** */
static int setup_ifname(int fd, const char *ifname, const char *ipaddr,
const char *netmask, uint8_t *mac, int mtu) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ-1] = '\0';
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(ifr.ifr_hwaddr.sa_data, mac, 6);
if(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFHWADDR) failed [%d]: %s", errno, strerror(errno));
return(-1);
}
ifr.ifr_addr.sa_family = AF_INET;
/* Interface Address */
inet_pton(AF_INET, ipaddr, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr);
if(ioctl(fd, SIOCSIFADDR, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFADDR) failed [%d]: %s", errno, strerror(errno));
return(-2);
}
/* Netmask */
if(netmask && (((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr.s_addr != 0)) {
inet_pton(AF_INET, netmask, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr);
if(ioctl(fd, SIOCSIFNETMASK, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFNETMASK, %s) failed [%d]: %s", netmask, errno, strerror(errno));
return(-3);
}
}
/* MTU */
ifr.ifr_mtu = mtu;
if(ioctl(fd, SIOCSIFMTU, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFMTU) failed [%d]: %s", errno, strerror(errno));
return(-4);
}
/* Set up and running */
if(ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCGIFFLAGS) failed [%d]: %s", errno, strerror(errno));
return(-5);
}
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
if(ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFFLAGS) failed [%d]: %s", errno, strerror(errno));
return(-6);
}
return(0);
}
/* ********************************** */
/** @brief Open and configure the TAP device for packet read/write.
*
* This routine creates the interface via the tuntap driver and then
* configures it.
*
* @param device - [inout] a device info holder object
* @param dev - user-defined name for the new iface,
* if NULL system will assign a name
* @param device_ip - address of iface
* @param device_mask - netmask for device_ip
* @param mtu - MTU for device_ip
*
* @return - negative value on error
* - non-negative file-descriptor on success
*/
int tuntap_open(tuntap_dev *device,
char *dev, /* user-definable interface name, eg. edge0 */
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu) {
char *tuntap_device = "/dev/net/tun";
int ioctl_fd;
struct ifreq ifr;
int rc;
int nl_fd;
char nl_buf[8192]; /* >= 8192 to avoid truncation, see "man 7 netlink" */
struct iovec iov;
struct sockaddr_nl sa;
int up_and_running = 0;
struct msghdr msg;
device->fd = open(tuntap_device, O_RDWR);
if(device->fd < 0) {
traceEvent(TRACE_ERROR, "tuntap open() error: %s[%d]. Is the tun kernel module loaded?\n", strerror(errno), errno);
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */
strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
ifr.ifr_name[IFNAMSIZ-1] = '\0';
rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr);
if(rc < 0) {
traceEvent(TRACE_ERROR, "tuntap ioctl(TUNSETIFF, IFF_TAP) error: %s[%d]\n", strerror(errno), rc);
close(device->fd);
return -1;
}
/* Store the device name for later reuse */
strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ) );
if(device_mac && device_mac[0]) {
/* Use the user-provided MAC */
str2mac(device->mac_addr, device_mac);
} else {
/* Set an explicit random MAC to know the exact MAC in use. Manually
* reading the MAC address is not safe as it may change internally
* also after the TAP interface UP status has been notified. */
int i;
for(i = 0; i < 6; i++)
device->mac_addr[i] = rand();
device->mac_addr[0] &= ~0x01; /* Clear multicast bit */
device->mac_addr[0] |= 0x02; /* Set locally-assigned bit */
}
/* Initialize Netlink socket */
if((nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) {
traceEvent(TRACE_ERROR, "netlink socket creation failed [%d]: %s", errno, strerror(errno));
return -1;
}
iov.iov_base = nl_buf;
iov.iov_len = sizeof(nl_buf);
memset(&sa, 0, sizeof(sa));
sa.nl_family = PF_NETLINK;
sa.nl_groups = RTMGRP_LINK;
sa.nl_pid = getpid();
msg.msg_name = &sa;
msg.msg_namelen = sizeof(sa);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
/* Subscribe to interface events */
if(bind(nl_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
traceEvent(TRACE_ERROR, "netlink socket bind failed [%d]: %s", errno, strerror(errno));
return -1;
}
if((ioctl_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
traceEvent(TRACE_ERROR, "socket creation failed [%d]: %s", errno, strerror(errno));
close(nl_fd);
return -1;
}
if(setup_ifname(ioctl_fd, device->dev_name, device_ip, device_mask, device->mac_addr, mtu) < 0) {
close(nl_fd);
close(ioctl_fd);
close(device->fd);
return -1;
}
close(ioctl_fd);
/* Wait for the up and running notification */
traceEvent(TRACE_INFO, "Waiting for TAP interface to be up and running...");
while(!up_and_running) {
ssize_t len = recvmsg(nl_fd, &msg, 0);
struct nlmsghdr *nh;
for(nh = (struct nlmsghdr *)nl_buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
if(nh->nlmsg_type == NLMSG_ERROR) {
traceEvent(TRACE_DEBUG, "nh->nlmsg_type == NLMSG_ERROR");
break;
}
if(nh->nlmsg_type == NLMSG_DONE)
break;
if(nh->nlmsg_type == NETLINK_GENERIC) {
struct ifinfomsg *ifi = NLMSG_DATA(nh);
/* NOTE: skipping interface name check, assuming it's our TAP */
if((ifi->ifi_flags & IFF_UP) && (ifi->ifi_flags & IFF_RUNNING)) {
up_and_running = 1;
traceEvent(TRACE_INFO, "Interface is up and running");
break;
}
}
}
}
close(nl_fd);
device->ip_addr = inet_addr(device_ip);
device->device_mask = inet_addr(device_mask);
return(device->fd);
}
/* *************************************************** */
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(read(tuntap->fd, buf, len));
}
/* *************************************************** */
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(write(tuntap->fd, buf, len));
}
/* *************************************************** */
void tuntap_close(struct tuntap_dev *tuntap) {
close(tuntap->fd);
}
/* *************************************************** */
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap) {
struct ifreq ifr;
int fd;
if((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
traceEvent(TRACE_ERROR, "socket creation failed [%d]: %s", errno, strerror(errno));
return;
}
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, tuntap->dev_name, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ-1] = '\0';
if(ioctl(fd, SIOCGIFADDR, &ifr) != -1)
tuntap->ip_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
close(fd);
}
#endif /* #ifdef __linux__ */