4 * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5 * featured implementation uAnytun has no support for multiple connections
6 * or synchronisation. It is a small single threaded implementation intended
7 * to act as a client on small platforms.
8 * The secure anycast tunneling protocol (satp) defines a protocol used
9 * for communication between any combination of unicast and anycast
10 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
11 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
12 * ethernet, ip, arp ...). satp directly includes cryptography and
13 * message authentication based on the methodes used by SRTP. It is
14 * intended to deliver a generic, scaleable and secure solution for
15 * tunneling and relaying of packets of any protocol.
18 * Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
20 * This file is part of uAnytun.
22 * uAnytun is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 3 as
24 * published by the Free Software Foundation.
26 * uAnytun is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
35 #include "datatypes.h"
47 #include "auth_algo.h"
50 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
51 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
54 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
55 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
58 #define PARSE_INT_PARAM(SHORT, LONG, VALUE) \
59 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
63 VALUE = atoi(argv[i+1]); \
68 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \
69 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
71 if(argc < 1 || argv[i+1][0] == '-') \
73 if(VALUE) free(VALUE); \
74 VALUE = strdup(argv[i+1]); \
81 #define PARSE_STRING_PARAM_SEC(SHORT, LONG, VALUE) \
82 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
84 if(argc < 1 || argv[i+1][0] == '-') \
86 if(VALUE) free(VALUE); \
87 VALUE = strdup(argv[i+1]); \
91 for(j=0; j < strlen(argv[i+1]); ++j) \
97 #define PARSE_IFCONFIG_PARAM(SHORT, LONG, VALUE) \
98 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
100 if(argc < 1 || argv[i+1][0] == '-') \
103 ret = options_parse_ifconfig(argv[i+1], &VALUE); \
112 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
113 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
115 if(argc < 1 || argv[i+1][0] == '-') \
118 ret = options_parse_hex_string(argv[i+1], &VALUE); \
124 for(j=0; j < strlen(argv[i+1]); ++j) \
125 argv[i+1][j] = '#'; \
130 #define PARSE_STRING_LIST(SHORT, LONG, LIST) \
131 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
133 if(argc < 1 || argv[i+1][0] == '-') \
135 int ret = string_list_add(&LIST, argv[i+1]); \
144 int options_parse_hex_string(const char* hex, buffer_t* buffer)
149 u_int32_t hex_len = strlen(hex);
156 buffer->length_ = hex_len/2;
157 buffer->buf_ = malloc(buffer->length_);
163 const char* ptr = hex;
165 for(i=0;i<buffer->length_;++i) {
167 sscanf(ptr, "%2X", &tmp);
168 buffer->buf_[i] = (u_int8_t)tmp;
175 int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg)
177 char* str = strdup(arg);
191 ifcfg->prefix_length_ = atoi(ptr);
192 ifcfg->net_addr_ = strdup(str);
195 if(!ifcfg->net_addr_)
200 if(!isdigit(*ptr) && *ptr != '.') {
211 int options_parse(options_t* opt, int argc, char* argv[])
216 options_default(opt);
219 free(opt->progname_);
220 opt->progname_ = strdup(argv[0]);
227 int i, ipv4_only = 0, ipv6_only = 0;
228 for(i=1; argc > 0; ++i)
233 if(!strcmp(str,"-h") || !strcmp(str,"--help"))
235 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", opt->daemonize_)
236 PARSE_STRING_PARAM("-u","--username", opt->username_)
237 PARSE_STRING_PARAM("-g","--groupname", opt->groupname_)
238 PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_)
239 PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_)
240 PARSE_STRING_PARAM("-i","--interface", opt->local_addr_)
241 PARSE_STRING_PARAM("-p","--port", opt->local_port_)
242 PARSE_INT_PARAM("-s","--sender-id", opt->sender_id_)
243 PARSE_STRING_LIST("-L","--log", opt->log_targets_)
244 PARSE_STRING_PARAM("-r","--remote-host", opt->remote_addr_)
245 PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_)
246 PARSE_BOOL_PARAM("-4","--ipv4-only", ipv4_only)
247 PARSE_BOOL_PARAM("-6","--ipv6-only", ipv6_only)
248 PARSE_STRING_PARAM("-d","--dev", opt->dev_name_)
249 PARSE_STRING_PARAM("-t","--type", opt->dev_type_)
250 PARSE_IFCONFIG_PARAM("-n","--ifconfig", opt->ifconfig_param_)
251 PARSE_STRING_PARAM("-x","--post-up-script", opt->post_up_script_)
252 PARSE_INT_PARAM("-m","--mux", opt->mux_)
253 PARSE_INT_PARAM("-w","--window-size", opt->seq_window_size_)
255 PARSE_STRING_PARAM("-k","--kd-prf", opt->kd_prf_)
256 #ifndef NO_PASSPHRASE
257 PARSE_STRING_PARAM_SEC("-E","--passphrase", opt->passphrase_)
259 PARSE_STRING_PARAM("-e","--role", role)
260 PARSE_HEXSTRING_PARAM_SEC("-K","--key", opt->key_)
261 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", opt->salt_)
262 PARSE_STRING_PARAM("-c","--cipher", opt->cipher_)
263 PARSE_STRING_PARAM("-a","--auth-algo", opt->auth_algo_)
264 PARSE_INT_PARAM("-b","--auth-tag-length", opt->auth_tag_length_)
269 if(ipv4_only && ipv6_only)
272 opt->resolv_addr_type_ = IPV4_ONLY;
274 opt->resolv_addr_type_ = IPV6_ONLY;
277 if(!strcmp(role, "alice") || !strcmp(role, "server") || !strcmp(role, "left"))
278 opt->role_ = ROLE_LEFT;
279 else if(!strcmp(role, "bob") || !strcmp(role, "client") || !strcmp(role, "right"))
280 opt->role_ = ROLE_RIGHT;
290 void options_parse_post(options_t* opt)
296 if(opt->resolv_addr_type_ == ANY) {
297 opt->resolv_addr_type_ = IPV4_ONLY;
298 log_printf(WARNING, "No support for V4-mapped Adresses on this platform, defaulting to only use IPv4 addresses");
303 if(!strcmp(opt->cipher_, "null") && !strcmp(opt->auth_algo_, "null") &&
304 strcmp(opt->kd_prf_, "null")) {
307 opt->kd_prf_ = strdup("null");
309 if((strcmp(opt->cipher_, "null") || strcmp(opt->auth_algo_, "null")) &&
310 !strcmp(opt->kd_prf_, "null")) {
311 log_printf(WARNING, "using NULL key derivation with encryption and or authentication enabled!");
314 u_int32_t tag_len_max = auth_algo_get_max_length(opt->auth_algo_);
315 if(!tag_len_max) opt->auth_tag_length_ = 0;
316 else if(tag_len_max < opt->auth_tag_length_) {
317 log_printf(WARNING, "%s auth algo can't generate tags of length %d, using maximum tag length(%d)", opt->auth_algo_, opt->auth_tag_length_, tag_len_max);
318 opt->auth_tag_length_ = tag_len_max;
322 if(!(opt->dev_name_) && !(opt->dev_type_))
323 opt->dev_type_ = strdup("tun");
326 void options_default(options_t* opt)
331 opt->progname_ = strdup("uanytun");
333 opt->username_ = NULL;
334 opt->groupname_ = NULL;
335 opt->chroot_dir_ = NULL;
336 opt->pid_file_ = NULL;
337 string_list_init(&opt->log_targets_);
338 opt->local_addr_ = NULL;
339 opt->local_port_ = strdup("4444");
341 opt->remote_addr_ = NULL;
342 opt->remote_port_ = strdup("4444");
343 opt->resolv_addr_type_ = ANY;
344 opt->dev_name_ = NULL;
345 opt->dev_type_ = NULL;
346 opt->ifconfig_param_.net_addr_ = NULL;
347 opt->ifconfig_param_.prefix_length_ = 0;
348 opt->post_up_script_ = NULL;
350 opt->seq_window_size_ = 0;
352 opt->kd_prf_ = strdup("aes-ctr");
353 opt->passphrase_ = NULL;
354 opt->role_ = ROLE_LEFT;
355 opt->cipher_ = strdup("aes-ctr");
356 opt->auth_algo_ = strdup("sha1");
357 opt->auth_tag_length_ = 10;
359 opt->cipher_ = strdup("null");
360 opt->auth_tag_length_ = 0;
362 opt->key_.buf_ = NULL;
363 opt->key_.length_ = 0;
364 opt->salt_.buf_ = NULL;
365 opt->salt_.length_ = 0;
368 void options_clear(options_t* opt)
374 free(opt->progname_);
376 free(opt->username_);
378 free(opt->groupname_);
380 free(opt->chroot_dir_);
382 free(opt->pid_file_);
383 string_list_clear(&opt->log_targets_);
385 free(opt->local_addr_);
387 free(opt->local_port_);
388 if(opt->remote_addr_)
389 free(opt->remote_addr_);
390 if(opt->remote_port_)
391 free(opt->remote_port_);
393 free(opt->dev_name_);
395 free(opt->dev_type_);
396 if(opt->ifconfig_param_.net_addr_)
397 free(opt->ifconfig_param_.net_addr_);
398 if(opt->post_up_script_)
399 free(opt->post_up_script_);
404 free(opt->auth_algo_);
408 free(opt->passphrase_);
411 free(opt->key_.buf_);
413 free(opt->salt_.buf_);
416 void options_print_usage()
419 printf("uanytun [-h|--help] prints this...\n");
420 printf(" [-D|--nodaemonize] don't run in background\n");
421 printf(" [-u|--username] <username> change to this user\n");
422 printf(" [-g|--groupname] <groupname> change to this group\n");
423 printf(" [-C|--chroot] <path> chroot to this directory\n");
424 printf(" [-P|--write-pid] <path> write pid to this file\n");
425 printf(" [-i|--interface] <ip-address> local ip address to bind to\n");
426 printf(" [-p|--port] <port> local port to bind to\n");
427 printf(" [-s|--sender-id ] <sender id> the sender id to use\n");
428 printf(" [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n");
429 printf(" add a log target, can be invoked several times\n");
431 printf(" [-r|--remote-host] <hostname|ip> remote host\n");
432 printf(" [-o|--remote-port] <port> remote port\n");
433 printf(" [-4|--ipv4-only] always resolv IPv4 addresses\n");
434 printf(" [-6|--ipv6-only] always resolv IPv6 addresses\n");
435 printf(" [-d|--dev] <name> device name\n");
436 printf(" [-t|--type] <tun|tap> device type\n");
438 printf(" [-n|--ifconfig] <local>/<prefix> the local address for the tun/tap device and the used prefix length\n");
439 printf(" [-x|--post-up-script] <script> script gets called after interface is created\n");
440 printf(" [-m|--mux] <mux-id> the multiplex id to use\n");
441 printf(" [-w|--window-size] <window size> seqence number window size\n");
443 printf(" [-k|--kd-prf] <kd-prf type> key derivation pseudo random function\n");
444 #ifndef NO_PASSPHRASE
445 printf(" [-E|--passphrase] <pass phrase> a passprhase to generate master key and salt from\n");
447 printf(" [-K|--key] <master key> master key to use for encryption\n");
448 printf(" [-A|--salt] <master salt> master salt to use for encryption\n");
449 printf(" [-e|--role] <role> left (alice) or right (bob)");
450 printf(" [-c|--cipher] <cipher type> payload encryption algorithm\n");
451 printf(" [-a|--auth-algo] <algo type> message authentication algorithm\n");
452 printf(" [-b|--auth-tag-length] <length> length of the auth tag\n");
456 void options_print(options_t* opt)
461 printf("progname: '%s'\n", opt->progname_);
462 printf("daemonize: %d\n", opt->daemonize_);
463 printf("username: '%s'\n", opt->username_);
464 printf("groupname: '%s'\n", opt->groupname_);
465 printf("chroot_dir: '%s'\n", opt->chroot_dir_);
466 printf("pid_file: '%s'\n", opt->pid_file_);
467 printf("log_targets: \n");
468 string_list_print(&opt->log_targets_, " '", "'\n");
469 printf("local_addr: '%s'\n", opt->local_addr_);
470 printf("local_port: '%s'\n", opt->local_port_);
471 printf("sender_id: %d\n", opt->sender_id_);
472 printf("remote_addr: '%s'\n", opt->remote_addr_);
473 printf("remote_port: '%s'\n", opt->remote_port_);
474 printf("resolv_addr_type: ");
475 switch(opt->resolv_addr_type_) {
476 case ANY: printf("any\n"); break;
477 case IPV4_ONLY: printf("ipv4-only\n"); break;
478 case IPV6_ONLY: printf("ipv6-only\n"); break;
479 default: printf("??\n"); break;
481 printf("dev_name: '%s'\n", opt->dev_name_);
482 printf("dev_type: '%s'\n", opt->dev_type_);
483 printf("ifconfig_net_addr: '%s'\n", opt->ifconfig_param_.net_addr_);
484 printf("ifconfig_prefix_length: %d\n", opt->ifconfig_param_.prefix_length_);
485 printf("post_up_script: '%s'\n", opt->post_up_script_);
486 printf("mux: %d\n", opt->mux_);
487 printf("seq_window_size: %d\n", opt->seq_window_size_);
488 printf("cipher: '%s'\n", opt->cipher_);
490 printf("auth_algo: '%s'\n", opt->auth_algo_);
491 printf("auth_tag_length: %d\n", opt->auth_tag_length_);
492 printf("kd_prf: '%s'\n", opt->kd_prf_);
493 printf("passphrase: '%s'\n", opt->passphrase_);
496 case ROLE_LEFT: printf("left\n"); break;
497 case ROLE_RIGHT: printf("right\n"); break;
498 default: printf("??\n"); break;
503 printf("key_[%d]: '", opt->key_.length_);
504 for(i=0; i<opt->key_.length_; ++i) printf("%02X", opt->key_.buf_[i]);
507 printf("salt_[%d]: '", opt->salt_.length_);
508 for(i=0; i<opt->salt_.length_; ++i) printf("%02X", opt->salt_.buf_[i]);