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 as published by
24 * the Free Software Foundation, either version 3 of the License, or
27 * uAnytun is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
36 #include "datatypes.h"
48 #include "auth_algo.h"
51 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
52 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
55 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
56 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
59 #define PARSE_INT_PARAM(SHORT, LONG, VALUE) \
60 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
64 VALUE = atoi(argv[i+1]); \
69 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \
70 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
72 if(argc < 1 || argv[i+1][0] == '-') \
74 if(VALUE) free(VALUE); \
75 VALUE = strdup(argv[i+1]); \
82 #define PARSE_STRING_PARAM_SEC(SHORT, LONG, VALUE) \
83 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
85 if(argc < 1 || argv[i+1][0] == '-') \
87 if(VALUE) free(VALUE); \
88 VALUE = strdup(argv[i+1]); \
92 for(j=0; j < strlen(argv[i+1]); ++j) \
98 #define PARSE_IFCONFIG_PARAM(SHORT, LONG, VALUE) \
99 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
101 if(argc < 1 || argv[i+1][0] == '-') \
104 ret = options_parse_ifconfig(argv[i+1], &VALUE); \
113 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
114 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
116 if(argc < 1 || argv[i+1][0] == '-') \
119 ret = options_parse_hex_string(argv[i+1], &VALUE); \
125 for(j=0; j < strlen(argv[i+1]); ++j) \
126 argv[i+1][j] = '#'; \
131 #define PARSE_STRING_LIST(SHORT, LONG, LIST) \
132 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
134 if(argc < 1 || argv[i+1][0] == '-') \
136 int ret = string_list_add(&LIST, argv[i+1]); \
145 int options_parse_hex_string(const char* hex, buffer_t* buffer)
150 u_int32_t hex_len = strlen(hex);
157 buffer->length_ = hex_len/2;
158 buffer->buf_ = malloc(buffer->length_);
164 const char* ptr = hex;
166 for(i=0;i<buffer->length_;++i) {
168 sscanf(ptr, "%2X", &tmp);
169 buffer->buf_[i] = (u_int8_t)tmp;
176 int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg)
178 char* str = strdup(arg);
192 ifcfg->prefix_length_ = atoi(ptr);
193 ifcfg->net_addr_ = strdup(str);
196 if(!ifcfg->net_addr_)
201 if(!isdigit(*ptr) && *ptr != '.') {
212 int options_parse(options_t* opt, int argc, char* argv[])
217 options_default(opt);
220 free(opt->progname_);
221 opt->progname_ = strdup(argv[0]);
228 int i, ipv4_only = 0, ipv6_only = 0;
229 for(i=1; argc > 0; ++i)
234 if(!strcmp(str,"-h") || !strcmp(str,"--help"))
236 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", opt->daemonize_)
237 PARSE_STRING_PARAM("-u","--username", opt->username_)
238 PARSE_STRING_PARAM("-g","--groupname", opt->groupname_)
239 PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_)
240 PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_)
241 PARSE_STRING_PARAM("-i","--interface", opt->local_addr_)
242 PARSE_STRING_PARAM("-p","--port", opt->local_port_)
243 PARSE_INT_PARAM("-s","--sender-id", opt->sender_id_)
244 PARSE_STRING_LIST("-L","--log", opt->log_targets_)
245 PARSE_STRING_PARAM("-r","--remote-host", opt->remote_addr_)
246 PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_)
247 PARSE_BOOL_PARAM("-4","--ipv4-only", ipv4_only)
248 PARSE_BOOL_PARAM("-6","--ipv6-only", ipv6_only)
249 PARSE_STRING_PARAM("-d","--dev", opt->dev_name_)
250 PARSE_STRING_PARAM("-t","--type", opt->dev_type_)
251 PARSE_IFCONFIG_PARAM("-n","--ifconfig", opt->ifconfig_param_)
252 PARSE_STRING_PARAM("-x","--post-up-script", opt->post_up_script_)
253 PARSE_INT_PARAM("-m","--mux", opt->mux_)
254 PARSE_INT_PARAM("-w","--window-size", opt->seq_window_size_)
256 PARSE_STRING_PARAM("-k","--kd-prf", opt->kd_prf_)
257 #ifndef NO_PASSPHRASE
258 PARSE_STRING_PARAM_SEC("-E","--passphrase", opt->passphrase_)
260 PARSE_STRING_PARAM("-e","--role", role)
261 PARSE_HEXSTRING_PARAM_SEC("-K","--key", opt->key_)
262 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", opt->salt_)
263 PARSE_STRING_PARAM("-c","--cipher", opt->cipher_)
264 PARSE_STRING_PARAM("-a","--auth-algo", opt->auth_algo_)
265 PARSE_INT_PARAM("-b","--auth-tag-length", opt->auth_tag_length_)
270 if(ipv4_only && ipv6_only)
273 opt->resolv_addr_type_ = IPV4_ONLY;
275 opt->resolv_addr_type_ = IPV6_ONLY;
279 if(!strcmp(role, "alice") || !strcmp(role, "server") || !strcmp(role, "left"))
280 opt->role_ = ROLE_LEFT;
281 else if(!strcmp(role, "bob") || !strcmp(role, "client") || !strcmp(role, "right"))
282 opt->role_ = ROLE_RIGHT;
293 void options_parse_post(options_t* opt)
299 if(opt->resolv_addr_type_ == ANY) {
300 opt->resolv_addr_type_ = IPV4_ONLY;
301 log_printf(WARNING, "No support for V4-mapped Adresses on this platform, defaulting to only use IPv4 addresses");
306 if(!strcmp(opt->cipher_, "null") && !strcmp(opt->auth_algo_, "null") &&
307 strcmp(opt->kd_prf_, "null")) {
310 opt->kd_prf_ = strdup("null");
312 if((strcmp(opt->cipher_, "null") || strcmp(opt->auth_algo_, "null")) &&
313 !strcmp(opt->kd_prf_, "null")) {
314 log_printf(WARNING, "using NULL key derivation with encryption and or authentication enabled!");
317 u_int32_t tag_len_max = auth_algo_get_max_length(opt->auth_algo_);
318 if(!tag_len_max) opt->auth_tag_length_ = 0;
319 else if(tag_len_max < opt->auth_tag_length_) {
320 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);
321 opt->auth_tag_length_ = tag_len_max;
325 if(!(opt->dev_name_) && !(opt->dev_type_))
326 opt->dev_type_ = strdup("tun");
329 void options_default(options_t* opt)
334 opt->progname_ = strdup("uanytun");
336 opt->username_ = NULL;
337 opt->groupname_ = NULL;
338 opt->chroot_dir_ = NULL;
339 opt->pid_file_ = NULL;
340 string_list_init(&opt->log_targets_);
341 opt->local_addr_ = NULL;
342 opt->local_port_ = strdup("4444");
344 opt->remote_addr_ = NULL;
345 opt->remote_port_ = strdup("4444");
346 opt->resolv_addr_type_ = ANY;
347 opt->dev_name_ = NULL;
348 opt->dev_type_ = NULL;
349 opt->ifconfig_param_.net_addr_ = NULL;
350 opt->ifconfig_param_.prefix_length_ = 0;
351 opt->post_up_script_ = NULL;
353 opt->seq_window_size_ = 0;
355 opt->kd_prf_ = strdup("aes-ctr");
356 opt->passphrase_ = NULL;
357 opt->role_ = ROLE_LEFT;
358 opt->cipher_ = strdup("aes-ctr");
359 opt->auth_algo_ = strdup("sha1");
360 opt->auth_tag_length_ = 10;
362 opt->cipher_ = strdup("null");
363 opt->auth_tag_length_ = 0;
365 opt->key_.buf_ = NULL;
366 opt->key_.length_ = 0;
367 opt->salt_.buf_ = NULL;
368 opt->salt_.length_ = 0;
371 void options_clear(options_t* opt)
377 free(opt->progname_);
379 free(opt->username_);
381 free(opt->groupname_);
383 free(opt->chroot_dir_);
385 free(opt->pid_file_);
386 string_list_clear(&opt->log_targets_);
388 free(opt->local_addr_);
390 free(opt->local_port_);
391 if(opt->remote_addr_)
392 free(opt->remote_addr_);
393 if(opt->remote_port_)
394 free(opt->remote_port_);
396 free(opt->dev_name_);
398 free(opt->dev_type_);
399 if(opt->ifconfig_param_.net_addr_)
400 free(opt->ifconfig_param_.net_addr_);
401 if(opt->post_up_script_)
402 free(opt->post_up_script_);
407 free(opt->auth_algo_);
411 free(opt->passphrase_);
414 free(opt->key_.buf_);
416 free(opt->salt_.buf_);
419 void options_print_usage()
422 printf("uanytun [-h|--help] prints this...\n");
423 printf(" [-D|--nodaemonize] don't run in background\n");
424 printf(" [-u|--username] <username> change to this user\n");
425 printf(" [-g|--groupname] <groupname> change to this group\n");
426 printf(" [-C|--chroot] <path> chroot to this directory\n");
427 printf(" [-P|--write-pid] <path> write pid to this file\n");
428 printf(" [-i|--interface] <ip-address> local ip address to bind to\n");
429 printf(" [-p|--port] <port> local port to bind to\n");
430 printf(" [-s|--sender-id ] <sender id> the sender id to use\n");
431 printf(" [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n");
432 printf(" add a log target, can be invoked several times\n");
434 printf(" [-r|--remote-host] <hostname|ip> remote host\n");
435 printf(" [-o|--remote-port] <port> remote port\n");
436 printf(" [-4|--ipv4-only] always resolv IPv4 addresses\n");
437 printf(" [-6|--ipv6-only] always resolv IPv6 addresses\n");
438 printf(" [-d|--dev] <name> device name\n");
439 printf(" [-t|--type] <tun|tap> device type\n");
441 printf(" [-n|--ifconfig] <local>/<prefix> the local address for the tun/tap device and the used prefix length\n");
442 printf(" [-x|--post-up-script] <script> script gets called after interface is created\n");
443 printf(" [-m|--mux] <mux-id> the multiplex id to use\n");
444 printf(" [-w|--window-size] <window size> seqence number window size\n");
446 printf(" [-k|--kd-prf] <kd-prf type> key derivation pseudo random function\n");
447 #ifndef NO_PASSPHRASE
448 printf(" [-E|--passphrase] <pass phrase> a passprhase to generate master key and salt from\n");
450 printf(" [-K|--key] <master key> master key to use for encryption\n");
451 printf(" [-A|--salt] <master salt> master salt to use for encryption\n");
452 printf(" [-e|--role] <role> left (alice) or right (bob)\n");
453 printf(" [-c|--cipher] <cipher type> payload encryption algorithm\n");
454 printf(" [-a|--auth-algo] <algo type> message authentication algorithm\n");
455 printf(" [-b|--auth-tag-length] <length> length of the auth tag\n");
459 void options_print(options_t* opt)
464 printf("progname: '%s'\n", opt->progname_);
465 printf("daemonize: %d\n", opt->daemonize_);
466 printf("username: '%s'\n", opt->username_);
467 printf("groupname: '%s'\n", opt->groupname_);
468 printf("chroot_dir: '%s'\n", opt->chroot_dir_);
469 printf("pid_file: '%s'\n", opt->pid_file_);
470 printf("log_targets: \n");
471 string_list_print(&opt->log_targets_, " '", "'\n");
472 printf("local_addr: '%s'\n", opt->local_addr_);
473 printf("local_port: '%s'\n", opt->local_port_);
474 printf("sender_id: %d\n", opt->sender_id_);
475 printf("remote_addr: '%s'\n", opt->remote_addr_);
476 printf("remote_port: '%s'\n", opt->remote_port_);
477 printf("resolv_addr_type: ");
478 switch(opt->resolv_addr_type_) {
479 case ANY: printf("any\n"); break;
480 case IPV4_ONLY: printf("ipv4-only\n"); break;
481 case IPV6_ONLY: printf("ipv6-only\n"); break;
482 default: printf("??\n"); break;
484 printf("dev_name: '%s'\n", opt->dev_name_);
485 printf("dev_type: '%s'\n", opt->dev_type_);
486 printf("ifconfig_net_addr: '%s'\n", opt->ifconfig_param_.net_addr_);
487 printf("ifconfig_prefix_length: %d\n", opt->ifconfig_param_.prefix_length_);
488 printf("post_up_script: '%s'\n", opt->post_up_script_);
489 printf("mux: %d\n", opt->mux_);
490 printf("seq_window_size: %d\n", opt->seq_window_size_);
491 printf("cipher: '%s'\n", opt->cipher_);
493 printf("auth_algo: '%s'\n", opt->auth_algo_);
494 printf("auth_tag_length: %d\n", opt->auth_tag_length_);
495 printf("kd_prf: '%s'\n", opt->kd_prf_);
496 printf("passphrase: '%s'\n", opt->passphrase_);
499 case ROLE_LEFT: printf("left\n"); break;
500 case ROLE_RIGHT: printf("right\n"); break;
501 default: printf("??\n"); break;
506 printf("key_[%d]: '", opt->key_.length_);
507 for(i=0; i<opt->key_.length_; ++i) printf("%02X", opt->key_.buf_[i]);
510 printf("salt_[%d]: '", opt->salt_.length_);
511 for(i=0; i<opt->salt_.length_; ++i) printf("%02X", opt->salt_.buf_[i]);