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-2010 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"
49 #include "auth_algo.h"
52 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
53 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
56 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
57 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
60 #define PARSE_INT_PARAM(SHORT, LONG, VALUE) \
61 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
65 VALUE = atoi(argv[i+1]); \
70 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \
71 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
73 if(argc < 1 || argv[i+1][0] == '-') \
75 if(VALUE) free(VALUE); \
76 VALUE = strdup(argv[i+1]); \
83 #define PARSE_STRING_PARAM_SEC(SHORT, LONG, VALUE) \
84 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
86 if(argc < 1 || argv[i+1][0] == '-') \
88 if(VALUE) free(VALUE); \
89 VALUE = strdup(argv[i+1]); \
93 for(j=0; j < strlen(argv[i+1]); ++j) \
99 #define PARSE_IFCONFIG_PARAM(SHORT, LONG, VALUE) \
100 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
102 if(argc < 1 || argv[i+1][0] == '-') \
105 ret = options_parse_ifconfig(argv[i+1], &VALUE); \
114 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
115 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
117 if(argc < 1 || argv[i+1][0] == '-') \
120 ret = options_parse_hex_string(argv[i+1], &VALUE); \
126 for(j=0; j < strlen(argv[i+1]); ++j) \
127 argv[i+1][j] = '#'; \
132 #define PARSE_STRING_LIST(SHORT, LONG, LIST) \
133 else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \
135 if(argc < 1 || argv[i+1][0] == '-') \
137 int ret = string_list_add(&LIST, argv[i+1]); \
146 int options_parse_hex_string(const char* hex, buffer_t* buffer)
151 u_int32_t hex_len = strlen(hex);
158 buffer->length_ = hex_len/2;
159 buffer->buf_ = malloc(buffer->length_);
165 const char* ptr = hex;
167 for(i=0;i<buffer->length_;++i) {
169 sscanf(ptr, "%2X", &tmp);
170 buffer->buf_[i] = (u_int8_t)tmp;
177 int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg)
179 char* str = strdup(arg);
193 ifcfg->prefix_length_ = atoi(ptr);
194 ifcfg->net_addr_ = strdup(str);
197 if(!ifcfg->net_addr_)
202 if(!isdigit(*ptr) && *ptr != '.') {
213 int options_parse(options_t* opt, int argc, char* argv[])
218 options_default(opt);
221 free(opt->progname_);
222 opt->progname_ = strdup(argv[0]);
229 int i, ipv4_only = 0, ipv6_only = 0;
230 for(i=1; argc > 0; ++i)
235 if(!strcmp(str,"-h") || !strcmp(str,"--help"))
237 else if(!strcmp(str,"-v") || !strcmp(str,"--version"))
239 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", opt->daemonize_)
240 PARSE_STRING_PARAM("-u","--username", opt->username_)
241 PARSE_STRING_PARAM("-g","--groupname", opt->groupname_)
242 PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_)
243 PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_)
244 PARSE_STRING_PARAM("-i","--interface", opt->local_addr_)
245 PARSE_STRING_PARAM("-p","--port", opt->local_port_)
246 PARSE_INT_PARAM("-s","--sender-id", opt->sender_id_)
247 PARSE_STRING_LIST("-L","--log", opt->log_targets_)
248 PARSE_BOOL_PARAM("-U", "--debug", opt->debug_)
249 PARSE_STRING_PARAM("-r","--remote-host", opt->remote_addr_)
250 PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_)
251 PARSE_BOOL_PARAM("-4","--ipv4-only", ipv4_only)
252 PARSE_BOOL_PARAM("-6","--ipv6-only", ipv6_only)
253 PARSE_STRING_PARAM("-d","--dev", opt->dev_name_)
254 PARSE_STRING_PARAM("-t","--type", opt->dev_type_)
255 PARSE_IFCONFIG_PARAM("-n","--ifconfig", opt->ifconfig_param_)
256 PARSE_STRING_PARAM("-x","--post-up-script", opt->post_up_script_)
257 PARSE_INT_PARAM("-m","--mux", opt->mux_)
258 PARSE_INT_PARAM("-w","--window-size", opt->seq_window_size_)
260 PARSE_STRING_PARAM("-k","--kd-prf", opt->kd_prf_)
261 #ifndef NO_PASSPHRASE
262 PARSE_STRING_PARAM_SEC("-E","--passphrase", opt->passphrase_)
264 PARSE_STRING_PARAM("-e","--role", role)
265 PARSE_HEXSTRING_PARAM_SEC("-K","--key", opt->key_)
266 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", opt->salt_)
267 PARSE_STRING_PARAM("-c","--cipher", opt->cipher_)
268 PARSE_STRING_PARAM("-a","--auth-algo", opt->auth_algo_)
269 PARSE_INT_PARAM("-b","--auth-tag-length", opt->auth_tag_length_)
274 if(ipv4_only && ipv6_only)
277 opt->resolv_addr_type_ = IPV4_ONLY;
279 opt->resolv_addr_type_ = IPV6_ONLY;
282 string_list_add(&opt->log_targets_, "stdout:5");
286 if(!opt->log_targets_.first_)
287 string_list_add(&opt->log_targets_, "syslog:3,uanytun,daemon");
291 if(!strcmp(role, "alice") || !strcmp(role, "server") || !strcmp(role, "left"))
292 opt->role_ = ROLE_LEFT;
293 else if(!strcmp(role, "bob") || !strcmp(role, "client") || !strcmp(role, "right"))
294 opt->role_ = ROLE_RIGHT;
305 void options_parse_post(options_t* opt)
311 if(!strcmp(opt->cipher_, "null") && !strcmp(opt->auth_algo_, "null") &&
312 strcmp(opt->kd_prf_, "null")) {
315 opt->kd_prf_ = strdup("null");
317 if((strcmp(opt->cipher_, "null") || strcmp(opt->auth_algo_, "null")) &&
318 !strcmp(opt->kd_prf_, "null")) {
319 log_printf(WARNING, "using NULL key derivation with encryption and or authentication enabled!");
322 u_int32_t tag_len_max = auth_algo_get_max_length(opt->auth_algo_);
323 if(!tag_len_max) opt->auth_tag_length_ = 0;
324 else if(tag_len_max < opt->auth_tag_length_) {
325 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);
326 opt->auth_tag_length_ = tag_len_max;
330 if(!(opt->dev_name_) && !(opt->dev_type_))
331 opt->dev_type_ = strdup("tun");
334 void options_default(options_t* opt)
339 opt->progname_ = strdup("uanytun");
341 opt->username_ = NULL;
342 opt->groupname_ = NULL;
343 opt->chroot_dir_ = NULL;
344 opt->pid_file_ = NULL;
345 string_list_init(&opt->log_targets_);
347 opt->local_addr_ = NULL;
348 opt->local_port_ = strdup("4444");
350 opt->remote_addr_ = NULL;
351 opt->remote_port_ = strdup("4444");
352 opt->resolv_addr_type_ = ANY;
353 opt->dev_name_ = NULL;
354 opt->dev_type_ = NULL;
355 opt->ifconfig_param_.net_addr_ = NULL;
356 opt->ifconfig_param_.prefix_length_ = 0;
357 opt->post_up_script_ = NULL;
359 opt->seq_window_size_ = 0;
361 opt->kd_prf_ = strdup("aes-ctr");
362 opt->passphrase_ = NULL;
363 opt->role_ = ROLE_LEFT;
364 opt->cipher_ = strdup("aes-ctr");
365 opt->auth_algo_ = strdup("sha1");
366 opt->auth_tag_length_ = 10;
368 opt->cipher_ = strdup("null");
369 opt->auth_tag_length_ = 0;
371 opt->key_.buf_ = NULL;
372 opt->key_.length_ = 0;
373 opt->salt_.buf_ = NULL;
374 opt->salt_.length_ = 0;
377 void options_clear(options_t* opt)
383 free(opt->progname_);
385 free(opt->username_);
387 free(opt->groupname_);
389 free(opt->chroot_dir_);
391 free(opt->pid_file_);
392 string_list_clear(&opt->log_targets_);
394 free(opt->local_addr_);
396 free(opt->local_port_);
397 if(opt->remote_addr_)
398 free(opt->remote_addr_);
399 if(opt->remote_port_)
400 free(opt->remote_port_);
402 free(opt->dev_name_);
404 free(opt->dev_type_);
405 if(opt->ifconfig_param_.net_addr_)
406 free(opt->ifconfig_param_.net_addr_);
407 if(opt->post_up_script_)
408 free(opt->post_up_script_);
413 free(opt->auth_algo_);
417 free(opt->passphrase_);
420 free(opt->key_.buf_);
422 free(opt->salt_.buf_);
425 void options_print_usage()
428 printf("uanytun [-h|--help] prints this...\n");
429 printf(" [-v|--version] print version info and exit\n");
430 printf(" [-D|--nodaemonize] don't run in background\n");
431 printf(" [-u|--username] <username> change to this user\n");
432 printf(" [-g|--groupname] <groupname> change to this group\n");
433 printf(" [-C|--chroot] <path> chroot to this directory\n");
434 printf(" [-P|--write-pid] <path> write pid to this file\n");
435 printf(" [-i|--interface] <ip-address> local ip address to bind to\n");
436 printf(" [-p|--port] <port> local port to bind to\n");
437 printf(" [-s|--sender-id ] <sender id> the sender id to use\n");
438 printf(" [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n");
439 printf(" add a log target, can be invoked several times\n");
440 printf(" [-U|--debug] don't daemonize and log to stdout with maximum log level\n");
442 printf(" [-r|--remote-host] <hostname|ip> remote host\n");
443 printf(" [-o|--remote-port] <port> remote port\n");
444 printf(" [-4|--ipv4-only] always resolv IPv4 addresses\n");
445 printf(" [-6|--ipv6-only] always resolv IPv6 addresses\n");
446 printf(" [-d|--dev] <name> device name\n");
447 printf(" [-t|--type] <tun|tap> device type\n");
449 printf(" [-n|--ifconfig] <local>/<prefix> the local address for the tun/tap device and the used prefix length\n");
450 printf(" [-x|--post-up-script] <script> script gets called after interface is created\n");
451 printf(" [-m|--mux] <mux-id> the multiplex id to use\n");
452 printf(" [-w|--window-size] <window size> seqence number window size\n");
454 printf(" [-k|--kd-prf] <kd-prf type> key derivation pseudo random function\n");
455 #ifndef NO_PASSPHRASE
456 printf(" [-E|--passphrase] <pass phrase> a passprhase to generate master key and salt from\n");
458 printf(" [-K|--key] <master key> master key to use for encryption\n");
459 printf(" [-A|--salt] <master salt> master salt to use for encryption\n");
460 printf(" [-e|--role] <role> left (alice) or right (bob)\n");
461 printf(" [-c|--cipher] <cipher type> payload encryption algorithm\n");
462 printf(" [-a|--auth-algo] <algo type> message authentication algorithm\n");
463 printf(" [-b|--auth-tag-length] <length> length of the auth tag\n");
467 void options_print_version()
469 printf("%s\n", VERSION_STRING_0);
470 printf("%s\n", VERSION_STRING_1);
473 void options_print(options_t* opt)
478 printf("progname: '%s'\n", opt->progname_);
479 printf("daemonize: %d\n", opt->daemonize_);
480 printf("username: '%s'\n", opt->username_);
481 printf("groupname: '%s'\n", opt->groupname_);
482 printf("chroot_dir: '%s'\n", opt->chroot_dir_);
483 printf("pid_file: '%s'\n", opt->pid_file_);
484 printf("log_targets: \n");
485 string_list_print(&opt->log_targets_, " '", "'\n");
486 printf("debug: %s\n", !opt->debug_ ? "false" : "true");
487 printf("local_addr: '%s'\n", opt->local_addr_);
488 printf("local_port: '%s'\n", opt->local_port_);
489 printf("sender_id: %d\n", opt->sender_id_);
490 printf("remote_addr: '%s'\n", opt->remote_addr_);
491 printf("remote_port: '%s'\n", opt->remote_port_);
492 printf("resolv_addr_type: ");
493 switch(opt->resolv_addr_type_) {
494 case ANY: printf("any\n"); break;
495 case IPV4_ONLY: printf("ipv4-only\n"); break;
496 case IPV6_ONLY: printf("ipv6-only\n"); break;
497 default: printf("??\n"); break;
499 printf("dev_name: '%s'\n", opt->dev_name_);
500 printf("dev_type: '%s'\n", opt->dev_type_);
501 printf("ifconfig_net_addr: '%s'\n", opt->ifconfig_param_.net_addr_);
502 printf("ifconfig_prefix_length: %d\n", opt->ifconfig_param_.prefix_length_);
503 printf("post_up_script: '%s'\n", opt->post_up_script_);
504 printf("mux: %d\n", opt->mux_);
505 printf("seq_window_size: %d\n", opt->seq_window_size_);
506 printf("cipher: '%s'\n", opt->cipher_);
508 printf("auth_algo: '%s'\n", opt->auth_algo_);
509 printf("auth_tag_length: %d\n", opt->auth_tag_length_);
510 printf("kd_prf: '%s'\n", opt->kd_prf_);
511 printf("passphrase: '%s'\n", opt->passphrase_);
514 case ROLE_LEFT: printf("left\n"); break;
515 case ROLE_RIGHT: printf("right\n"); break;
516 default: printf("??\n"); break;
521 printf("key_[%d]: '", opt->key_.length_);
522 for(i=0; i<opt->key_.length_; ++i) printf("%02X", opt->key_.buf_[i]);
525 printf("salt_[%d]: '", opt->salt_.length_);
526 for(i=0; i<opt->salt_.length_; ++i) printf("%02X", opt->salt_.buf_[i]);