summaryrefslogtreecommitdiffhomepage
path: root/gre.c
blob: dff0a99cec0170995443a36f3d42fc67a221a8af (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * gre.c - userspace GRE tunnel
 *
 * Copyright (C) 2015 - 2017, Xiaoxiao <i@pxx.io>
 * Copyright (C) 2019 - 2020, Mikael Magnusson <mikma@users.sourceforge.net>
 *
 * 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 <getopt.h>
#include <linux/if_tun.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netdb.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_storage remote;
static size_t remote_len;
static short type = IFF_TUN;

uint8_t buf[4096];

static void gre_cb(void);
static void gre_ipv4(const uint8_t *buf, int n);
static void gre_ipv6(const uint8_t *buf, int n, const struct sockaddr_in6 *src);
static void gre_any(const uint8_t *buf, int n);
static int tun_cb(void);
static int tun_new(short type, const char *dev);
static int setnonblock(int fd);
static int runas(const char *user);
static int daemonize(void);
static int inet_addr_storage(const char *cp, struct sockaddr_storage *sp, size_t *sp_len);

int main(int argc, char **argv)
{
    fd_set readset;

    while (1)
    {
        int option_index = 0;
        static struct option long_options[] = {
            {"tun", no_argument, 0,  'n' },
            {"tap", no_argument, 0,  'p' },
            {0,     0,           0,  0 }
        };

        char c = getopt_long(argc, argv, "np",
                             long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
        case 'n':
            printf("tun\n");
            type = IFF_TUN;
            break;

        case 'p':
            printf("tap\n");
            type = IFF_TAP;
            break;

        case '?':
            break;

        default:
            printf("?? getopt returned character code 0%o ??\n", c);
        }
    }

    if ( (argc - optind) != 3)
    {
        printf("usage: %s [-n|--tun|-p|--tap] <tun> remote local\n", argv[0]);
        return EXIT_FAILURE;
    }

    const char *dev = argv[optind++];
    const char *remote_opt = argv[optind++];
    const char *local_opt = argv[optind++];

    tun = tun_new(type, dev);
    if (tun < 0)
    {
        printf("failed to init %s device\n", type == IFF_TAP ? "tap" : "tun");
        return EXIT_FAILURE;
    }

    struct sockaddr_storage local;
    size_t local_len = 0;
    if (inet_addr_storage(local_opt, &local, &local_len))
    {
        fprintf(stderr, "bad local address\n");
        return EXIT_FAILURE;
    }

    sock = socket(local.ss_family, SOCK_RAW, IPPROTO_GRE);
    if (sock < 0)
    {
        perror("socket");
        return EXIT_FAILURE;
    }

    if (bind(sock, (struct sockaddr *)&local, local_len) != 0)
    {
        perror("bind");
        return EXIT_FAILURE;
    }

    if (inet_addr_storage(remote_opt, &remote, &remote_len))
    {
        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))
        {
            if (tun_cb() < 0)
                return 0;
        }
    }

    return 0;
}

static void gre_cb(void)
{
    int n;
    struct sockaddr_storage src;
    socklen_t src_len = sizeof(src);

    memset(&src, 0, src_len);
    n = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)&src, &src_len);
    if (n < 0)
    {
        perror("recv");
        return;
    }

    switch (remote.ss_family) {
        case AF_INET: gre_ipv4(buf, n); break;
        case AF_INET6: gre_ipv6(buf, n, (const struct sockaddr_in6*)&src); break;
    }
}

static void gre_ipv4(const uint8_t *buf, int n)
{
    int ihl;    // IP header length

    ihl = 4 * (buf[0] & 0x0f);
    if (ihl > 60 || ihl < 20)
    {
        printf("IPv4 header too long\n");
        return;
    }
    // check source IPv4 address
    const struct sockaddr_in *remote_in = (const struct sockaddr_in *)&remote;
    if (*(uint32_t *)(buf + 12) != remote_in->sin_addr.s_addr)
    {
        return;
    }

    gre_any(buf + ihl, n - ihl);
}

static void gre_ipv6(const uint8_t *buf, int n, const struct sockaddr_in6 *src)
{
    if (n < 40)
    {
        return;
    }
    // check source IPv6 address
    const struct sockaddr_in6 *remote_in6 = (const struct sockaddr_in6 *)&remote;
    if (memcmp(src->sin6_addr.s6_addr, remote_in6->sin6_addr.s6_addr, 16) != 0)
    {
        return;
    }

    gre_any(buf, n);
}

static void gre_any(const uint8_t *buf, int n)
{
    // parse GRE header
    if (*(uint16_t *)(buf) != 0)
    {
        return;
    }
    uint16_t protocol = ntohs(*(uint16_t *)(buf + 2));
    if (protocol != ETHERTYPE_IP && protocol != ETHERTYPE_IPV6)
    {
        return;
    }

    write(tun, buf, n);
}

static int tun_cb(void)
{
    int n;

    n = read(tun, buf, sizeof(buf));
    if (n < 0)
    {
        int err = errno;
        perror("read");
        if (err == EBADFD)
            return -1;

        return 0;
    }
    uint16_t proto = ntohs(*(uint16_t *)(buf + 2));
    uint8_t *frame;

    if (type == IFF_TAP)
    {
        size_t ethsize = 6 + 6 + 2;
        frame = buf + ethsize;
        n = n - ethsize;
        frame[2] = buf[2];
        frame[3] = buf[3];
    } else {
        frame = buf;
    }

    frame[0] = 0;
    frame[1] = 0;
    switch (proto) {
    case ETHERTYPE_IP:
    case ETHERTYPE_IPV6:
        break;
    case ETHERTYPE_ARP:
        if (type == IFF_TAP)
            break;
        return 0;
    default:
        return 0;
    }
    sendto(sock, frame, n, 0, (struct sockaddr *)&remote, remote_len);
    return 0;
}

static int tun_new(short type, 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 = type;
    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;
}

static int inet_addr_storage(const char *cp, struct sockaddr_storage *sp, size_t *sp_len)
{
    struct addrinfo hints;
    struct addrinfo *result = NULL;
    int res;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_flags = AI_NUMERICHOST | AI_ADDRCONFIG;
    res = getaddrinfo(cp, NULL, &hints, &result);
    if (res != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
        return -1;
    }

    memcpy(sp, result->ai_addr, result->ai_addrlen);
    *sp_len = result->ai_addrlen;

    freeaddrinfo(result);
    result = NULL;

    if (sp->ss_family == AF_INET) {
        struct sockaddr_in *sin = (struct sockaddr_in *)sp;
        sin->sin_port = htons(IPPROTO_GRE);
    } else if (sp->ss_family == AF_INET6) {
        struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sp;
        sin->sin6_port = htons(IPPROTO_GRE);
    }

    return 0;
}