tun.c revision d26ecccd0dbd8a0139ae9ca6f72c44057b9a1b3c
1#define _GNU_SOURCE
2
3#include <arpa/inet.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <linux/if_tun.h>
7#include <net/if.h>
8#include <netinet/in.h>
9#include <netinet/ip.h>
10#include <sched.h>
11#include <stdarg.h>
12#include <stdint.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/ioctl.h>
17#include <sys/socket.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20#include <unistd.h>
21
22void HF_ITER(uint8_t**, size_t*);
23
24void fatal(const char* fmt, ...)
25{
26    fprintf(stdout, "[-] ");
27
28    va_list args;
29    va_start(args, fmt);
30    vfprintf(stdout, fmt, args);
31    va_end(args);
32
33    fprintf(stdout, "\n");
34
35    exit(1);
36}
37
38void pfatal(const char* fmt, ...)
39{
40    fprintf(stdout, "[-] ");
41
42    va_list args;
43    va_start(args, fmt);
44    vfprintf(stdout, fmt, args);
45    va_end(args);
46
47    fprintf(stdout, ": %s\n", strerror(errno));
48
49    exit(1);
50}
51
52void mlog(const char* fmt, ...)
53{
54    fprintf(stdout, "[+] ");
55
56    va_list args;
57    va_start(args, fmt);
58    vfprintf(stdout, fmt, args);
59    va_end(args);
60
61    fprintf(stdout, "\n");
62}
63
64int main(void)
65{
66    if (unshare(CLONE_NEWUSER | CLONE_NEWNET) == -1) {
67        pfatal("unshare()");
68    }
69
70    struct ifreq ifr;
71    memset(&ifr, '\0', sizeof(ifr));
72    ifr.ifr_flags = IFF_TUN | IFF_NO_PI | IFF_NOFILTER;
73    strcpy(ifr.ifr_name, "FUZZ0");
74
75    int fd = open("/dev/net/tun", O_RDWR);
76    if (fd == -1) {
77        pfatal("open('/dev/net/tun')");
78    }
79    if (ioctl(fd, TUNSETIFF, (void*)&ifr) != 0) {
80        pfatal("ioctl(TUNSETIFF)");
81    }
82    if (ioctl(fd, TUNSETNOCSUM, 1) != 0) {
83        pfatal("ioctl(TUNSETNOCSUM)");
84    }
85
86    int udp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
87    if (udp_sock == -1) {
88        pfatal("socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)");
89    }
90    int tcp_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
91    if (tcp_sock == -1) {
92        pfatal("socket(AF_INET, SOCK_STREAM, IPPROTO_IP)");
93    }
94    int sctp_sock = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
95    if (sctp_sock == -1) {
96        pfatal("socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)");
97    }
98
99    struct sockaddr_in* sa = (struct sockaddr_in*)(&ifr.ifr_addr);
100    sa->sin_family = AF_INET;
101    sa->sin_addr.s_addr = inet_addr("192.168.255.1");
102    if (ioctl(tcp_sock, SIOCSIFADDR, &ifr) == -1) {
103        pfatal("ioctl(tcp_sock, SIOCSIFADDR, &ifr)");
104    }
105    sa->sin_addr.s_addr = inet_addr("192.168.255.2");
106    if (ioctl(tcp_sock, SIOCSIFDSTADDR, &ifr) == -1) {
107        pfatal("ioctl(tcp_sock, SIOCSIFDSTADDR, &ifr)");
108    }
109
110    if (ioctl(tcp_sock, SIOCGIFFLAGS, &ifr) == -1) {
111        pfatal("ioctl(tcp_sock, SIOCGIFFLAGS, &ifr)");
112    }
113    ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
114    if (ioctl(tcp_sock, SIOCSIFFLAGS, &ifr) == -1) {
115        pfatal("ioctl(tcp_sock, SIOCSIFFLAGS, &ifr)");
116    }
117
118    struct sockaddr_in addr = {
119        .sin_family = AF_INET,
120        .sin_port = htons(1337),
121        .sin_addr.s_addr = INADDR_ANY,
122    };
123
124    if (bind(tcp_sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
125        pfatal("bind(tcp)");
126    }
127    if (bind(udp_sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
128        pfatal("bind(udp)");
129    }
130    if (bind(sctp_sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
131        pfatal("bind(sctp)");
132    }
133    if (fcntl(fd, F_SETFL, O_NONBLOCK | O_RDWR) == -1) {
134        pfatal("fcntl(fd, F_SETFL, O_NONBLOCK|O_RDWR)");
135    }
136    if (fcntl(tcp_sock, F_SETFL, O_NONBLOCK | O_RDWR) == -1) {
137        pfatal("fcntl(tcp_sock, F_SETFL, O_NONBLOCK|O_RDWR)");
138    }
139    if (fcntl(udp_sock, F_SETFL, O_NONBLOCK | O_RDWR) == -1) {
140        pfatal("fcntl(udp_sock, F_SETFL, O_NONBLOCK|O_RDWR)");
141    }
142    if (fcntl(sctp_sock, F_SETFL, O_NONBLOCK | O_RDWR) == -1) {
143        pfatal("fcntl(sctp_sock, F_SETFL, O_NONBLOCK|O_RDWR)");
144    }
145
146    if (listen(tcp_sock, SOMAXCONN) == -1) {
147        pfatal("listen(tcp_sock)");
148    }
149    if (listen(sctp_sock, SOMAXCONN) == -1) {
150        pfatal("listen(sctp_sock)");
151    }
152
153    int tcp_acc_sock = -1;
154
155    for (;;) {
156        uint8_t* buf;
157        size_t len;
158
159        HF_ITER(&buf, &len);
160
161        while (len > 0) {
162            size_t tlen = (len > 1400) ? 1400 : len;
163            write(fd, buf, tlen);
164            len -= tlen;
165        }
166
167        char b[1024 * 128];
168        for (;;) {
169            if (read(fd, b, sizeof(b)) <= 0) {
170                break;
171            }
172        }
173
174        if (tcp_acc_sock == -1) {
175            struct sockaddr_in nsock;
176            socklen_t slen = sizeof(nsock);
177            tcp_acc_sock = accept4(tcp_sock, (struct sockaddr*)&nsock, &slen, SOCK_NONBLOCK);
178        }
179        if (tcp_acc_sock != -1) {
180            if (recv(tcp_acc_sock, b, sizeof(b), MSG_DONTWAIT) == 0) {
181                close(tcp_acc_sock);
182                tcp_acc_sock = -1;
183            }
184            send(tcp_acc_sock, b, 1, MSG_NOSIGNAL | MSG_DONTWAIT);
185        }
186
187        struct sockaddr_in addr;
188        socklen_t slen = sizeof(addr);
189        if (recvfrom(udp_sock, b, sizeof(b), MSG_DONTWAIT, (struct sockaddr*)&addr, &slen) > 0) {
190            sendto(udp_sock, b, 1, MSG_NOSIGNAL | MSG_DONTWAIT, (struct sockaddr*)&addr, slen);
191        }
192
193        slen = sizeof(addr);
194        if (recvfrom(sctp_sock, b, sizeof(b), MSG_DONTWAIT, (struct sockaddr*)&addr, &slen) > 0) {
195            sendto(sctp_sock, b, 1, MSG_NOSIGNAL | MSG_DONTWAIT, (struct sockaddr*)&addr, slen);
196        }
197    }
198}
199