summaryrefslogtreecommitdiff
path: root/src/tools/ovpn/ovpn.c
blob: 02501d52fedb8dfaadf27ccda1251b509173af36 (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
/*
 * Ouroboros - Copyright (C) 2016 - 2020
 *
 * Ouroboros VPN
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@ugent.be>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define _POSIX_C_SOURCE 200109L

#include <ouroboros/dev.h>

#include <stdio.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <getopt.h>

#define BUF_SIZE 65536

int t_fd;
int o_fd;

static void usage(void)
{
        printf("Usage: ovpn [OPTION]...\n"
               "Sends TCP/IP traffic over Ouroboros\n\n"
               "  -n, --name                Run as client, name of ovpn "
               "server to connect to\n"
               "  -i, --ip                  IP address to give to TUN device\n"
               "  -m, --mask                Subnet mask to give to TUN device\n"
               "  -C, --crypt               AES encryption (default: off)\n"
               "\n"
               "      --help                Display this help text and exit\n");
}

static int tun_open(char *   dev,
                    uint32_t ip,
                    uint32_t mask)
{
        struct ifreq         ifr;
        int                  fd;
        int                  s;
        int                  ret;
        char *               clonedev = "/dev/net/tun";
        struct sockaddr_in * addr;

        fd = open(clonedev, O_RDWR);
        if (fd < 0)
                return -1;

        memset(&ifr, 0, sizeof(ifr));

        ifr.ifr_flags = IFF_TUN | IFF_NO_PI;

        ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
        if (ret < 0)
                goto fail;

        strcpy(dev, ifr.ifr_name);

        /* Now get the i/f up and running. */
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0)
                goto fail;

        /* Set IP address. */
        ifr.ifr_addr.sa_family = AF_INET;
        addr = (struct sockaddr_in *) &ifr.ifr_addr;
        addr->sin_addr.s_addr = ip;

        if (ioctl(s, SIOCSIFADDR, &ifr))
                goto fail_ioctl;

        /* Set subnet mask. */
        addr->sin_addr.s_addr = mask;
        if (ioctl(s, SIOCSIFNETMASK, &ifr))
                goto fail_ioctl;

        /* Bring interface up. */
        if (ioctl(s, SIOCGIFFLAGS, &ifr))
                goto fail_ioctl;
        ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
        if (ioctl(s, SIOCSIFFLAGS, &ifr))
                goto fail_ioctl;

        close(s);

        return fd;

 fail_ioctl:
        close(s);
 fail:
        close(fd);
        return -1;
}

void * o_reader(void * o)
{
        char buf[BUF_SIZE];
        int  len = 0;

        (void) o;

        while (true) {
                len = flow_read(o_fd, buf, BUF_SIZE);
                if (len <= 0)
                        continue;

                if (write(t_fd, buf, len))
                        continue;
        }
}

void * t_reader(void * o)
{
        char buf[BUF_SIZE];
        int  len = 0;

        (void) o;

        while (true) {
                len = read(t_fd, buf, BUF_SIZE);
                if (len <= 0)
                        continue;

                if (flow_write(o_fd, buf, len) < 0)
                        continue;
        }
}

static int check_mask(uint32_t mask)
{
        if (mask == 0)
                return 0;

        return ((~mask & (~mask + 1)) != 0);
}

int main(int     argc,
         char ** argv)
{
        char *    name = NULL;
        uint32_t  ip   = 0;
        int32_t   mask = -1;
        char      dev[IFNAMSIZ];
        pthread_t t_thr;
        pthread_t o_thr;
        sigset_t  sigset;
        int       sig;
        int       c;
        qosspec_t qs;

        static struct option long_options[] =
                {{"ip",    required_argument, NULL, 'i'},
                 {"mask",  required_argument, NULL, 'm'},
                 {"name",  required_argument, NULL, 'n'},
                 {"crypt", no_argument,       NULL, 'C'},
                 {"help",  no_argument,       NULL, 'h'},
                 {NULL,    0,                 NULL, 0}
                };

        sigemptyset(&sigset);
        sigaddset(&sigset, SIGINT);
        sigaddset(&sigset, SIGQUIT);
        sigaddset(&sigset, SIGHUP);
        sigaddset(&sigset, SIGTERM);

        if (geteuid() != 0) {
                printf("ovpn must be run as root.\n");
                exit(EXIT_FAILURE);
        }

        qs = qos_raw;

        while ((c = getopt_long(argc, argv, "i:m:n:Ch",
                                long_options, NULL)) != -1) {
                switch (c) {
                case 'i':
                        if (inet_pton(AF_INET, optarg, &ip) != 1) {
                                printf("Invalid IP address: %s.\n\n", optarg);
                                goto fail_usage;
                        }
                        break;
                case 'm':
                        if (inet_pton(AF_INET, optarg, &mask) != 1 ||
                            check_mask(htonl(mask))) {
                                printf("Invalid subnet mask: %s.\n\n", optarg);
                                goto fail_usage;
                        }
                        break;
                case 'n':
                        name = optarg;
                        break;
                case 'C':
                        qs = qos_raw_crypt;
                        break;
                case 'h':
                        usage();
                        exit(EXIT_SUCCESS);
                default:
                        exit(EXIT_FAILURE);
                }
        }

        if (optind < argc) {
                printf("Unknown arguments specified: ");
                while (optind < argc)
                        printf("%s ", argv[optind++]);
                printf("\n\n");
                goto fail_usage;
        }

        if (ip == 0) {
                printf("Please specify an IP address.\n\n");
                goto fail_usage;
        }

        if (mask == -1) {
                printf("Please specify a subnetmask.\n\n");
                goto fail_usage;
        }

        if (name != NULL) {
                printf("Allocating a flow to %s.\n", name);

                o_fd = flow_alloc(name, &qs, NULL);
                if (o_fd < 0) {
                        printf("Failed to allocate flow.\n");
                        goto fail_alloc;
                }
        } else {
                printf("Waiting for a new flow...\n");

                o_fd = flow_accept(NULL, NULL);
                if (o_fd < 0) {
                        printf("Failed to accept flow.\n");
                        goto fail_alloc;
                }
        }

        printf("Flow allocated.\n");

        t_fd = tun_open(dev, ip, mask);
        if (t_fd < 0) {
                printf("Failed to open tunnel device.\n");
                goto fail_tun;
        }

        printf("Tunnel device name is %s.\n", dev);

        pthread_sigmask(SIG_BLOCK, &sigset, NULL);

        printf("Starting read/write threads.\n");

        if (pthread_create(&o_thr, NULL, o_reader, NULL))
                goto fail_thread;

        if (pthread_create(&t_thr, NULL, t_reader, NULL))
                goto fail_thread2;

        while (true) {
                if (sigwait(&sigset, &sig) != 0) {
                        printf("Bad signal.\n");
                        continue;
                }

                printf("Shutting down...\n");
                break;
        }

        pthread_cancel(o_thr);
        pthread_cancel(t_thr);

        pthread_join(o_thr, NULL);
        pthread_join(t_thr, NULL);

        close(t_fd);
        flow_dealloc(o_fd);

        pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);

        exit(EXIT_SUCCESS);
 fail_usage:
        usage();
        exit(EXIT_FAILURE);
 fail_thread2:
        pthread_cancel(o_thr);
        pthread_join(o_thr, NULL);
 fail_thread:
        close(t_fd);
 fail_tun:
        flow_dealloc(o_fd);
 fail_alloc:
        exit(EXIT_FAILURE);
}