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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
Copyright (c) 2012-2014, Matthias Schiffer <mschiffer@universe-factory.net>
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.
*/
/**
\file
\em fastd main header file defining most data structures
*/
#pragma once
#include "alloc.h"
#include "dlist.h"
#include "buffer.h"
#include "log.h"
#include "shell.h"
#include "vector.h"
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <poll.h>
#include <semaphore.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/** An ethernet address */
struct __attribute__((__packed__)) fastd_eth_addr {
uint8_t data[ETH_ALEN]; /**< The bytes of the address */
};
/**
A structure describing callbacks that define a handshake protocol
Currently, only one such protocol, \em ec25519-fhmqvc, is defined.
*/
struct fastd_protocol {
/** The name of the procotol */
const char *name;
/** Performs one-time initialization tasks for the protocol */
fastd_protocol_config_t* (*init)(void);
/** Does some basic checks for the validity of a peer configuration */
void (*peer_verify)(fastd_peer_config_t *peer_conf);
/** Initializes protocol-specific parts of a peer configuration */
void (*peer_configure)(fastd_peer_config_t *peer_conf);
/** Checks if a peer configuration is valid and a connection may be established */
bool (*peer_check)(fastd_peer_config_t *peer_conf);
/** Checks if a temporary peer is valid and a connection may be established */
bool (*peer_check_temporary)(fastd_peer_t *peer);
/** Sends a handshake to the given peer */
void (*handshake_init)(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer);
/** Handles a handshake for the given peer */
void (*handshake_handle)(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_info_t *method);
#ifdef WITH_VERIFY
/** Handles an asynchrounous on-verify command return */
void (*handle_verify_return)(fastd_peer_t *peer, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, const fastd_method_info_t *method, const void *protocol_data, bool ok);
#endif
/** Handles a received payload packet (performs decryption and validity check, etc.) */
void (*handle_recv)(fastd_peer_t *peer, fastd_buffer_t buffer);
/** Sends a payload data packet to the given peer */
void (*send)(fastd_peer_t *peer, fastd_buffer_t buffer);
/** Initializes the protocol state for a peer */
void (*init_peer_state)(fastd_peer_t *peer);
/** Resets the protocol state for a peer (resets active sessions etc.) */
void (*reset_peer_state)(fastd_peer_t *peer);
/** Frees the protocol state for a peer */
void (*free_peer_state)(fastd_peer_t *peer);
/** Generates a new keypair and outputs it */
void (*generate_key)(void);
/** Outputs the public key for the configured secret */
void (*show_key)(void);
/** Adds peer-specific environment variables to env */
void (*set_shell_env)(fastd_shell_env_t *env, const fastd_peer_t *peer);
/** Creates a human-readable representation of the peer */
bool (*describe_peer)(const fastd_peer_t *peer, char *buf, size_t len);
};
/** An union storing an IPv4 or IPv6 address */
union fastd_peer_address {
struct sockaddr sa; /**< A sockaddr field (for access to sa_family) */
struct sockaddr_in in; /**< An IPv4 address */
struct sockaddr_in6 in6; /**< An IPv6 address */
};
/** A linked list of addresses to bind to */
struct fastd_bind_address {
fastd_bind_address_t *next; /**< The next address in the list */
fastd_peer_address_t addr; /**< The address to bind to */
char *bindtodev; /**< May contain an interface name to limit the bind to */
};
/** A socket descriptor */
struct fastd_socket {
int fd; /**< The file descriptor for the socket */
const fastd_bind_address_t *addr; /**< The address this socket is supposed to be bound to (or NULL) */
fastd_peer_address_t *bound_addr; /**< The actual address that was bound to (may differ from addr when addr has a random port) */
fastd_peer_t *peer; /**< If the socket belongs to a single peer (as it was create dynamically when sending a handshake), contains that peer */
};
/** Some kind of network transfer stratistics */
struct fastd_stats {
uint64_t packets; /**< The number of packets transferred */
uint64_t bytes; /**< The number of bytes transferred */
};
/** A data structure keeping track of an unknown addresses that a handshakes was received from recently */
struct fastd_handshake_timeout {
fastd_peer_address_t address; /**< An address a handshake was received from */
struct timespec timeout; /**< Timeout until handshakes from this address are ignored */
};
/** The static configuration of \em fastd */
struct fastd_config {
fastd_loglevel_t log_stderr_level; /**< The minimum loglevel of messages to print to stderr (or -1 to not print any messages on stderr) */
fastd_loglevel_t log_syslog_level; /**< The minimum loglevel of messages to print to syslog (or -1 to not print any messages on syslog) */
char *log_syslog_ident; /**< The identification string for messages sent to syslog (default: "fastd") */
char *ifname; /**< The configured interface name */
size_t n_bind_addrs; /**< Number of elements in bind_addrs */
fastd_bind_address_t *bind_addrs; /**< Configured bind addresses */
fastd_bind_address_t *bind_addr_default_v4; /**< Pointer to the bind address to be used for IPv4 connections by default */
fastd_bind_address_t *bind_addr_default_v6; /**< Pointer to the bind address to be used for IPv6 connections by default */
uint16_t mtu; /**< The configured MTU */
fastd_mode_t mode; /**< The configured mode of operation */
uint32_t packet_mark; /**< The configured packet mark (or 0) */
bool forward; /**< Specifies if packet forwarding is enable */
fastd_tristate_t pmtu; /**< Can be set to explicitly enable or disable PMTU detection */
bool secure_handshakes; /**< Can be set to false to support connections with fastd versions before v11 */
fastd_drop_caps_t drop_caps; /**< Specifies if and when to drop capabilities */
char *user; /**< Specifies which user to switch to after initialization */
char *group; /**< Can specify an alternative group to switch to */
uid_t uid; /**< The UID of the configured user */
gid_t gid; /**< The GID of the configured group */
size_t n_groups; /**< The number of supplementary groups of the user */
gid_t *groups; /**< The supplementary groups of the configured user */
const fastd_protocol_t *protocol; /**< The handshake protocol */
fastd_string_stack_t *method_list; /**< The list of configured method names */
fastd_method_info_t *methods; /**< The list of configured methods */
size_t max_overhead; /**< The maximum overhead of all configured methods */
size_t min_encrypt_head_space; /**< The minimum space a configured methods needs a the beginning of a buffer to encrypt */
size_t min_decrypt_head_space; /**< The minimum space a configured methods needs a the beginning of a buffer to decrypt */
size_t min_encrypt_tail_space; /**< The minimum space a configured methods needs a the end of a buffer to encrypt */
size_t min_decrypt_tail_space; /**< The minimum space a configured methods needs a the end of a buffer to decrypt */
char *secret; /**< The configured secret key */
fastd_peer_group_t *peer_group; /**< The root peer group configuration */
fastd_peer_config_t *peers; /**< The configured peers */
bool has_floating; /**< Specifies if any of the configured peers have floating remotes */
fastd_protocol_config_t *protocol_config; /**< The protocol-specific configuration */
fastd_shell_command_t on_pre_up; /**< The command to execute before the initialization of the tunnel interface */
fastd_shell_command_t on_up; /**< The command to execute after the initialization of the tunnel interface */
fastd_shell_command_t on_down; /**< The command to execute before the destruction of the tunnel interface */
fastd_shell_command_t on_post_down; /**< The command to execute after the destruction of the tunnel interface */
fastd_shell_command_t on_connect; /**< The command to execute before a handshake is sent to establish a new connection */
fastd_shell_command_t on_establish; /**< The command to execute when a new connection has been established */
fastd_shell_command_t on_disestablish; /**< The command to execute when a connection has been disestablished */
#ifdef WITH_VERIFY
fastd_shell_command_t on_verify; /**< The command to execute to check if a connection from an unknown peer should be allowed */
#endif
bool daemon; /**< Set to make fastd fork to the background after initialization */
char *pid_file; /**< A filename to write fastd's PID to */
bool hide_ip_addresses; /**< Tells fastd to hide peers' IP address in the log output */
bool hide_mac_addresses; /**< Tells fastd to hide peers' MAC address in the log output */
bool machine_readable; /**< Supresses explanatory messages in the generate_key and show_key commands */
bool generate_key; /**< Makes fastd generate a new keypair and exit */
bool show_key; /**< Makes fastd output the public key for the configured secret and exit */
bool verify_config; /**< Does basic verification of the configuration and exits */
};
/** The dynamic state of \em fastd */
struct fastd_context {
bool log_initialized; /**< true if the logging facilities have been properly initialized */
char *ifname; /**< The actual interface name */
struct timespec now; /**< The current monotonous timestamp */
uint64_t next_peer_id; /**< An monotonously increasing ID peers are identified with in some components */
VECTOR(fastd_peer_t*) peers; /**< The currectly active peers */
#ifdef WITH_VERIFY
sem_t verify_limit; /**< Keeps track of the number of verifier threads */
#endif
#ifdef USE_EPOLL
int epoll_fd; /**< The file descriptor for the epoll facility */
#else
VECTOR(struct pollfd) pollfds; /**< The vector of pollfds for all file descriptors */
#endif
uint32_t peer_addr_ht_seed; /**< The hash seed used for peer_addr_ht */
VECTOR(fastd_peer_t*) *peer_addr_ht; /**< An array of hash buckets for the peer hash table */
fastd_dlist_head_t handshake_queue; /**< A doubly linked list of the peers currently queued for handshakes (ordered by the time of the next handshake) */
struct timespec next_maintenance; /**< The time of the next maintenance call */
VECTOR(pid_t) async_pids; /**< PIDs of asynchronously executed commands which still have to be reaped */
int async_rfd; /**< The read side of the pipe used to send data from other thread to the main thread */
int async_wfd; /**< The write side of the pipe used to send data from other thread to the main thread */
pthread_attr_t detached_thread; /**< pthread_attr_t for creating detached threads */
int tunfd; /**< The file descriptor of the tunnel interface */
size_t n_socks; /**< The number of sockets in socks */
fastd_socket_t *socks; /**< Array of all sockets */
fastd_socket_t *sock_default_v4; /**< Points to the socket that is used for new outgoing IPv4 connections */
fastd_socket_t *sock_default_v6; /**< Points to the socket that is used for new outgoing IPv6 connections */
fastd_stats_t rx; /**< Reception statistics */
fastd_stats_t tx; /**< Transmission statistics (OK) */
fastd_stats_t tx_dropped; /**< Transmission statistics (dropped because of full queues) */
fastd_stats_t tx_error; /**< Transmission statistics (other errors) */
VECTOR(fastd_peer_eth_addr_t) eth_addrs; /**< Sorted vector of all known ethernet addresses with associated peers and timeouts */
size_t unknown_handshake_pos; /**< Current start position in the ring buffer unknown_handshakes */
fastd_handshake_timeout_t unknown_handshakes[8]; /**< Ring buffer of unknown addresses handshakes have been received from */
fastd_protocol_state_t *protocol_state; /**< Protocol-specific state */
};
/** A stack of strings */
struct fastd_string_stack {
fastd_string_stack_t *next; /**< The next element of the stack */
char str[]; /**< Zero-terminated character data */
};
extern fastd_context_t ctx;
extern fastd_config_t conf;
void fastd_send(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer, size_t stat_size);
void fastd_send_handshake(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer);
void fastd_send_data(fastd_buffer_t buffer, fastd_peer_t *source);
void fastd_receive(fastd_socket_t *sock);
void fastd_handle_receive(fastd_peer_t *peer, fastd_buffer_t buffer);
void fastd_close_all_fds(void);
bool fastd_socket_handle_binds(void);
fastd_socket_t* fastd_socket_open(fastd_peer_t *peer, int af);
void fastd_socket_close(fastd_socket_t *sock);
void fastd_socket_error(fastd_socket_t *sock);
void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote);
void fastd_tuntap_open(void);
void fastd_tuntap_handle(void);
void fastd_tuntap_write(fastd_buffer_t buffer);
void fastd_tuntap_close(void);
void fastd_cap_init(void);
void fastd_cap_drop(void);
void fastd_random_bytes(void *buffer, size_t len, bool secure);
/** Returns a random number between \a min (inclusively) and \a max (exclusively) */
static inline int fastd_rand(int min, int max) {
unsigned int r = (unsigned int)random();
return (r%(max-min) + min);
}
/** Sets the O_NONBLOCK flag on a file descriptor */
static inline void fastd_setnonblock(int fd) {
int flags = fcntl(fd, F_GETFL);
if (flags < 0)
exit_errno("Getting file status flags failed: fcntl");
if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0)
exit_errno("Setting file status flags failed: fcntl");
}
/**
Returns a pointer to a data structure, given the address of a member contained in the structure
@param ptr the address of the member
@param type the type of the container
@param member the name of the member
\hideinitializer
*/
#define container_of(ptr, type, member) ({ \
const __typeof__(((type *)0)->member) *_mptr = (ptr); \
(type*)((char*)_mptr - offsetof(type, member)); \
})
/**
Returns the number of elements of an array
\hideinitializer
*/
#define array_size(array) (sizeof(array)/sizeof((array)[0]))
/**
Determines how many blocks of a given size \a a are needed to contain some length \a l
*/
static inline size_t block_count(size_t l, size_t a) {
return (l+a-1)/a;
}
/**
Rounds up a length \a l to the next multiple of a block size \a a
*/
static inline size_t alignto(size_t l, size_t a) {
return block_count(l, a)*a;
}
/** Returns the maximum payload size \em fastd is configured to transport */
static inline size_t fastd_max_payload(void) {
switch (conf.mode) {
case MODE_TAP:
return conf.mtu+ETH_HLEN;
case MODE_TUN:
return conf.mtu;
default:
exit_bug("invalid mode");
}
}
/** Checks if a fastd_peer_address_t is an IPv6 link-local address */
static inline bool fastd_peer_address_is_v6_ll(const fastd_peer_address_t *addr) {
return (addr->sa.sa_family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&addr->in6.sin6_addr));
}
/** Duplicates a string, creating a one-element string stack */
static inline fastd_string_stack_t* fastd_string_stack_dup(const char *str) {
fastd_string_stack_t *ret = fastd_alloc(alignto(sizeof(fastd_string_stack_t) + strlen(str) + 1, 8));
ret->next = NULL;
strcpy(ret->str, str);
return ret;
}
/** Duplicates a string of a given maximum length, creating a one-element string stack */
static inline fastd_string_stack_t* fastd_string_stack_dupn(const char *str, size_t len) {
size_t str_len = strnlen(str, len);
fastd_string_stack_t *ret = fastd_alloc(alignto(sizeof(fastd_string_stack_t) + str_len + 1, 8));
ret->next = NULL;
strncpy(ret->str, str, str_len);
ret->str[str_len] = 0;
return ret;
}
/** Pushes the copy of a string onto the top of a string stack */
static inline fastd_string_stack_t* fastd_string_stack_push(fastd_string_stack_t *stack, const char *str) {
fastd_string_stack_t *ret = fastd_alloc(alignto(sizeof(fastd_string_stack_t) + strlen(str) + 1, 8));
ret->next = stack;
strcpy(ret->str, str);
return ret;
}
/** Frees a whole string stack */
static inline void fastd_string_stack_free(fastd_string_stack_t *str) {
while (str) {
fastd_string_stack_t *next = str->next;
free(str);
str = next;
}
}
/** Compares two timespecs and returns \em true if \p tp1 is after \p tp2 */
static inline bool timespec_after(const struct timespec *tp1, const struct timespec *tp2) {
return (tp1->tv_sec > tp2->tv_sec ||
(tp1->tv_sec == tp2->tv_sec && tp1->tv_nsec > tp2->tv_nsec));
}
/** Returns (\a tp1 - \a tp2) in milliseconds */
static inline int timespec_diff(const struct timespec *tp1, const struct timespec *tp2) {
return ((tp1->tv_sec - tp2->tv_sec))*1000 + (tp1->tv_nsec - tp2->tv_nsec)/1e6;
}
/**
Checks if a timeout has occured
@param timeout the time the timeout should occur
@return true if the given timeout is before or equal to the current time
\note The current time is updated only once per main loop iteration, after waiting for input.
*/
static inline bool fastd_timed_out(const struct timespec *timeout) {
return !timespec_after(timeout, &ctx.now);
}
/** Returns a timespec that lies a given number of seconds in the future */
static inline struct timespec fastd_in_seconds(const int seconds) {
struct timespec ret = ctx.now;
ret.tv_sec += seconds;
return ret;
}
/** Updates the current time */
static inline void fastd_update_time(void) {
clock_gettime(CLOCK_MONOTONIC, &ctx.now);
}
/** Checks if a on-verify command is set */
static inline bool fastd_allow_verify(void) {
#ifdef WITH_VERIFY
return fastd_shell_command_isset(&conf.on_verify);
#else
return false;
#endif
}
/**
Checks if two strings are equal
@param str1 The first string (may be NULL)
@param str2 The second string (may be NULL)
@return True if both strings are NULL or both strings are not NULL and equal
*/
static inline bool strequal(const char *str1, const char *str2) {
if (str1 && str2)
return (!strcmp(str1, str2));
else
return (str1 == str2);
}
/** Returns the maximum of two size_t values */
static inline size_t max_size_t(size_t a, size_t b) {
return (a > b) ? a : b;
}
/** Returns the minimum of two size_t values */
static inline size_t min_size_t(size_t a, size_t b) {
return (a < b) ? a : b;
}
|