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
|
/*
* gre.c - userspace GRE tunnel
*
* Copyright (C) 2015, Xiaoxiao <i@xiaoxiao.im>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
static int tun;
static int sock;
static struct sockaddr_in remote;
uint8_t buf[4096];
static void gre_cb(void);
static void tun_cb(void);
static int tun_new(const char *dev);
static int setnonblock(int fd);
static int runas(const char *user);
static int daemonize(void);
int main(int argc, char **argv)
{
fd_set readset;
if (argc != 4)
{
printf("usage: %s <tun> remote local\n", argv[0]);
return EXIT_FAILURE;
}
tun = tun_new(argv[1]);
if (tun < 0)
{
printf("failed to init tun device\n");
return EXIT_FAILURE;
}
sock = socket(AF_INET, SOCK_RAW, IPPROTO_GRE);
if (sock < 0)
{
perror("socket");
return EXIT_FAILURE;
}
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(IPPROTO_GRE);
local.sin_addr.s_addr = inet_addr(argv[3]);
if (local.sin_addr.s_addr == INADDR_NONE)
{
fprintf(stderr, "bad local address\n");
return EXIT_FAILURE;
}
else
{
if (bind(sock, (struct sockaddr *)&local, sizeof(local)) != 0)
{
perror("bind");
return EXIT_FAILURE;
}
}
remote.sin_family = AF_INET;
remote.sin_port = htons(IPPROTO_GRE);
remote.sin_addr.s_addr = inet_addr(argv[2]);
if (remote.sin_addr.s_addr == INADDR_NONE)
{
fprintf(stderr, "bad remote address\n");
return EXIT_FAILURE;
}
setnonblock(sock);
setnonblock(tun);
runas("nobody");
daemonize();
int maxfd = (tun > sock ? tun : sock) + 1;
while (1)
{
FD_ZERO(&readset);
FD_SET(tun, &readset);
FD_SET(sock, &readset);
int r = select(maxfd, &readset, NULL, NULL, NULL);
if (r < 0)
{
if (errno == EINTR)
{
continue;
}
else
{
perror("select");
break;
}
}
if (FD_ISSET(sock, &readset))
{
gre_cb();
}
if (FD_ISSET(tun, &readset))
{
tun_cb();
}
}
return 0;
}
static void gre_cb(void)
{
int ihl; // IP header length
int n;
n = recv(sock, buf, sizeof(buf), 0);
if (n < 0)
{
perror("recv");
return;
}
ihl = 4 * (buf[0] & 0x0f);
if (ihl > 60 || ihl < 20)
{
printf("IPv4 header too long\n");
return;
}
// check source IPv4 address
if (*(uint32_t *)(buf + 12) != remote.sin_addr.s_addr)
{
return;
}
// parse GRE header
if (*(uint16_t *)(buf + ihl) != 0)
{
return;
}
uint16_t protocol = ntohs(*(uint16_t *)(buf + ihl + 2));
if (protocol != 0x0800)
{
return;
}
write(tun, buf + ihl + 4, n - ihl - 4);
}
static void tun_cb(void)
{
int n;
n = read(tun, buf + 4, sizeof(buf));
if (n < 0)
{
perror("read");
return;
}
*(uint16_t *)(buf) = 0;
*(uint16_t *)(buf + 2) = htons(0x0800);
sendto(sock, buf, n + 4, 0, (struct sockaddr *)&remote, sizeof(struct sockaddr));
}
static int tun_new(const char *dev)
{
struct ifreq ifr;
int fd, err;
fd = open("/dev/net/tun", O_RDWR);
if (fd < 0)
{
return -1;
}
bzero(&ifr, sizeof(struct ifreq));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if (*dev != '\0')
{
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
err = ioctl(fd, TUNSETIFF, (void *)&ifr);
if (err < 0)
{
return err;
}
return fd;
}
static int setnonblock(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
{
return -1;
}
if (-1 == fcntl(fd, F_SETFL, flags | O_NONBLOCK))
{
return -1;
}
return 0;
}
static int runas(const char *user)
{
struct passwd *pw_ent = getpwnam(user);
if (pw_ent != NULL)
{
if (setegid(pw_ent->pw_gid) != 0)
{
return -1;
}
if (seteuid(pw_ent->pw_uid) != 0)
{
return -1;
}
}
return 0;
}
static int daemonize(void)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("fork");
return -1;
}
if (pid > 0)
{
exit(0);
}
umask(0);
if (setsid() < 0)
{
perror("setsid");
return -1;
}
return 0;
}
|