aboutsummaryrefslogtreecommitdiffstats
path: root/cbits/tun-macos.c
blob: 7d5735085e677a8bcd1c56245653882432369755 (plain) (blame)
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
/**
  Copyright (C) 2015 clowwindy

  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/>.
*/

// Extracted from ShadowVPN project, with some minor modifications.

#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include <net/if_utun.h>
#include <netinet/ip.h>
#include <sys/ioctl.h>
#include <sys/kern_control.h>
#include <sys/sys_domain.h>
#include <sys/uio.h>
#include <unistd.h>

#include "tun.h"

int tun_alloc(const char *dev_name, int threads, int *fds) {
    struct ctl_info ctlInfo;
    struct sockaddr_ctl sc;
    int fd, utun_num;

    if (!dev_name || sscanf(dev_name, "utun%d", &utun_num) != 1)
        return -1;

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

    if (strlcpy(ctlInfo.ctl_name,
                UTUN_CONTROL_NAME,
                sizeof(ctlInfo.ctl_name)) >= sizeof(ctlInfo.ctl_name)) {
        return -1;
    }

    fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);

    if (fd < 0)
        return -1;

    if (ioctl(fd, CTLIOCGINFO, &ctlInfo) < 0) {
        close(fd);
        return -1;
    }

    sc.sc_id = ctlInfo.ctl_id;
    sc.sc_len = sizeof(sc);
    sc.sc_family = AF_SYSTEM;
    sc.ss_sysaddr = AF_SYS_CONTROL;

    sc.sc_unit = utun_num + 1;

    if (connect(fd, (struct sockaddr*)&sc, sizeof(sc)) < 0) {
        close(fd);
        return -1;
    }

    *fds = fd;

    return 1;
}

inline int utun_modified_len(int len) {
    if (len > 0)
        return (len > sizeof(u_int32_t)) ? len - sizeof(u_int32_t) : 0;
    else
        return len;
}

int utun_read(int fd, void *buf, size_t len) {
    u_int32_t type;
    struct iovec iv[2];

    iv[0].iov_base = &type;
    iv[0].iov_len = sizeof(type);
    iv[1].iov_base = buf;
    iv[1].iov_len = len;

    return utun_modified_len(readv(fd, iv, 2));
}

int utun_write(int fd, void *buf, size_t len) {
    u_int32_t type;
    struct iovec iv[2];
    struct ip *iph;

    iph = (struct ip *) buf;

    if (iph->ip_v == 6)
        type = htonl(AF_INET6);
    else
        type = htonl(AF_INET);

    iv[0].iov_base = &type;
    iv[0].iov_len = sizeof(type);
    iv[1].iov_base = buf;
    iv[1].iov_len = len;

    return utun_modified_len(writev(fd, iv, 2));
}