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
|
/*
Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
Partly based on QuickTun Copyright (c) 2010, Ivo Smits <Ivo@UCIS.nl>.
All rights reserved.
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.
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.
*/
#include "fastd.h"
#include "handshake.h"
#include "task.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
extern fastd_method fastd_method_null;
static void init_tuntap(fastd_context *ctx) {
struct ifreq ifr;
pr_debug(ctx, "Initializing tun/tap device...");
if ((ctx->tunfd = open("/dev/net/tun", O_RDWR)) < 0)
exit_errno(ctx, "Could not open tun/tap device file");
memset(&ifr, 0, sizeof(ifr));
if (ctx->conf->ifname)
strncpy(ifr.ifr_name, ctx->conf->ifname, IF_NAMESIZE-1);
ifr.ifr_flags = IFF_TAP;
ifr.ifr_flags |= IFF_NO_PI;
if (ioctl(ctx->tunfd, TUNSETIFF, (void *)&ifr) < 0)
exit_errno(ctx, "TUNSETIFF ioctl failed");
pr_debug(ctx, "Tun/tap device initialized.");
}
static void init_socket(fastd_context *ctx) {
pr_debug(ctx, "Initializing UDP socket...");
if ((ctx->sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
exit_errno(ctx, "socket");
struct sockaddr_in bindaddr;
bindaddr.sin_family = AF_INET;
bindaddr.sin_addr.s_addr = ctx->conf->bind_address;
bindaddr.sin_port = ctx->conf->bind_port;
if (bind(ctx->sockfd, (struct sockaddr*)&bindaddr, sizeof(struct sockaddr_in)))
exit_errno(ctx, "bind");
pr_debug(ctx, "UDP socket initialized.");
}
static void configure(fastd_context *ctx, fastd_config *conf) {
conf->loglevel = LOG_DEBUG;
conf->ifname = NULL;
conf->bind_address = htonl(INADDR_ANY);
conf->bind_port = htons(1337);
conf->mtu = 1500;
conf->protocol = PROTOCOL_ETHERNET;
conf->method = &fastd_method_null;
conf->peers = malloc(sizeof(fastd_peer_config));
conf->peers->next = NULL;
conf->peers->address = inet_addr("172.22.184.1");
conf->peers->port = htons(1337);
ctx->peers = NULL;
}
static void init_peers(fastd_context *ctx) {
fastd_peer **current_peer = &ctx->peers;
fastd_peer_config *peer_conf;
for (peer_conf = ctx->conf->peers; peer_conf; peer_conf = peer_conf->next) {
*current_peer = malloc(sizeof(fastd_peer));
(*current_peer)->next = NULL;
(*current_peer)->config = peer_conf;
(*current_peer)->address = peer_conf->address;
(*current_peer)->port = peer_conf->port;
(*current_peer)->state = STATE_WAIT;
(*current_peer)->last_req_id = 0;
(*current_peer)->addresses = NULL;
fastd_task_schedule_handshake(ctx, *current_peer, 0);
current_peer = &(*current_peer)->next;
}
}
static void* get_source_address(const fastd_context *ctx, void *buffer) {
switch (ctx->conf->protocol) {
case PROTOCOL_ETHERNET:
return &((struct ethhdr*)buffer)->h_source;
case PROTOCOL_IP:
return NULL;
default:
exit_bug(ctx, "invalid protocol");
}
}
static void* get_dest_address(const fastd_context *ctx, void *buffer) {
switch (ctx->conf->protocol) {
case PROTOCOL_ETHERNET:
return &((struct ethhdr*)buffer)->h_dest;
case PROTOCOL_IP:
return NULL;
default:
exit_bug(ctx, "invalid protocol");
}
}
static void handle_tasks(fastd_context *ctx) {
fastd_task *task;
while ((task = fastd_task_get(ctx)) != NULL) {
switch (task->type) {
case TASK_SEND:
if (task->send.peer) {
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
struct sockaddr_in sendaddr;
sendaddr.sin_family = AF_INET;
sendaddr.sin_addr.s_addr = task->send.peer->address;
sendaddr.sin_port = task->send.peer->port;
msg.msg_name = &sendaddr;
msg.msg_namelen = sizeof(sendaddr);
struct iovec iov[2] = {
{ .iov_base = &task->send.packet_type, .iov_len = 1 },
{ .iov_base = task->send.buffer.base, .iov_len = task->send.buffer.len }
};
msg.msg_iov = iov;
msg.msg_iovlen = task->send.buffer.len ? 2 : 1;
sendmsg(ctx->sockfd, &msg, 0);
}
fastd_buffer_free(task->send.buffer);
break;
case TASK_HANDLE_RECV:
// TODO Handle source address
write(ctx->tunfd, task->handle_recv.buffer.base, task->handle_recv.buffer.len);
fastd_buffer_free(task->handle_recv.buffer);
break;
case TASK_HANDSHAKE:
if (task->handshake.peer->state != STATE_WAIT)
break;
pr_debug(ctx, "Sending handshake...");
fastd_handshake_send(ctx, task->handshake.peer);
fastd_task_schedule_handshake(ctx, task->handshake.peer, 20000);
break;
default:
exit_bug(ctx, "invalid task type");
}
free(task);
}
}
static void handle_input(fastd_context *ctx) {
struct pollfd fds[2];
fds[0].fd = ctx->tunfd;
fds[0].events = POLLIN;
fds[1].fd = ctx->sockfd;
fds[1].events = POLLIN;
int ret = poll(fds, 2, fastd_task_timeout(ctx));
if (ret < 0)
exit_errno(ctx, "poll");
if (fds[0].revents & POLLIN) {
size_t max_len = fastd_max_packet_size(ctx);
fastd_buffer buffer = fastd_buffer_alloc(max_len, 0);
ssize_t len = read(ctx->tunfd, buffer.base, max_len);
if (len < 0)
exit_errno(ctx, "read");
uint8_t *src_addr = get_source_address(ctx, buffer.base);
uint8_t *dest_addr = get_dest_address(ctx, buffer.base);
pr_debug(ctx, "A packet with length %u is to be sent from %02x:%02x:%02x:%02x:%02x:%02x to %02x:%02x:%02x:%02x:%02x:%02x",
(unsigned)len, src_addr[0], src_addr[1], src_addr[2], src_addr[3], src_addr[4], src_addr[5],
dest_addr[0], dest_addr[1], dest_addr[2], dest_addr[3], dest_addr[4], dest_addr[5]);
// TODO find correct peer
fastd_peer *peer = ctx->peers;
if (peer->state == STATE_ESTABLISHED) {
buffer.len = len;
ctx->conf->method->method_send(ctx, peer, buffer);
}
else {
fastd_buffer_free(buffer);
}
}
if (fds[1].revents & POLLIN) {
size_t max_len = ctx->conf->method->method_max_packet_size(ctx);
fastd_buffer buffer = fastd_buffer_alloc(max_len, 0);
uint8_t packet_type;
struct iovec iov[2] = {
{ .iov_base = &packet_type, .iov_len = 1 },
{ .iov_base = buffer.base, .iov_len = max_len }
};
struct sockaddr_in recvaddr;
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
msg.msg_name = &recvaddr;
msg.msg_namelen = sizeof(recvaddr);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
ssize_t len = recvmsg(ctx->sockfd, &msg, 0);
if (len < 0)
pr_warn(ctx, "recvfrom: %s", strerror(errno));
// TODO get correct peer
fastd_peer *peer = ctx->peers;
switch (packet_type) {
case 0:
buffer.len = len - 1;
ctx->conf->method->method_handle_recv(ctx, peer, buffer);
break;
case 1:
fastd_handshake_handle(ctx, peer, buffer);
break;
default:
fastd_buffer_free(buffer);
}
}
}
int main() {
fastd_context ctx;
memset(&ctx, 0, sizeof(ctx));
fastd_config conf;
configure(&ctx, &conf);
ctx.conf = &conf;
init_peers(&ctx);
init_tuntap(&ctx);
init_socket(&ctx);
while (1) {
handle_tasks(&ctx);
handle_input(&ctx);
}
return 0;
}
|