… | |
… | |
32 | #include <sys/stat.h> |
32 | #include <sys/stat.h> |
33 | #include <sys/types.h> |
33 | #include <sys/types.h> |
34 | #include <unistd.h> |
34 | #include <unistd.h> |
35 | |
35 | |
36 | #include <netinet/in.h> |
36 | #include <netinet/in.h> |
|
|
37 | #include <arpa/inet.h> |
|
|
38 | #ifdef ENABLE_ICMP |
|
|
39 | # ifdef HAVE_NETINET_IN_SYSTM_H |
|
|
40 | # include <netinet/in_systm.h> |
|
|
41 | # endif |
|
|
42 | # ifdef HAVE_NETINET_IP_H |
|
|
43 | # include <netinet/ip.h> |
|
|
44 | # endif |
|
|
45 | # include <netinet/ip_icmp.h> |
|
|
46 | #endif |
37 | |
47 | |
38 | #include <openssl/err.h> |
48 | #include <openssl/err.h> |
39 | #include <openssl/pem.h> |
49 | #include <openssl/pem.h> |
40 | #include <openssl/rsa.h> |
50 | #include <openssl/rsa.h> |
41 | #include <openssl/rand.h> |
51 | #include <openssl/rand.h> |
… | |
… | |
53 | |
63 | |
54 | struct configuration conf; |
64 | struct configuration conf; |
55 | |
65 | |
56 | u8 best_protocol (u8 protset) |
66 | u8 best_protocol (u8 protset) |
57 | { |
67 | { |
58 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
68 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
|
|
69 | if (protset & PROT_ICMPv4) return PROT_ICMPv4; |
59 | if (protset & PROT_UDPv4) return PROT_UDPv4; |
70 | if (protset & PROT_UDPv4 ) return PROT_UDPv4; |
60 | if (protset & PROT_TCPv4) return PROT_TCPv4; |
71 | if (protset & PROT_TCPv4 ) return PROT_TCPv4; |
61 | |
72 | |
62 | return 0; |
73 | return 0; |
63 | } |
74 | } |
64 | |
75 | |
65 | const char *strprotocol (u8 protocol) |
76 | const char *strprotocol (u8 protocol) |
66 | { |
77 | { |
67 | if (protocol & PROT_IPv4 ) return "rawip"; |
78 | if (protocol & PROT_IPv4 ) return "rawip"; |
|
|
79 | if (protocol & PROT_ICMPv4) return "icmp"; |
68 | if (protocol & PROT_UDPv4) return "udp"; |
80 | if (protocol & PROT_UDPv4 ) return "udp"; |
|
|
81 | if (protocol & PROT_TCPv4 ) return "tcp"; |
69 | |
82 | |
70 | return "<unknown>"; |
83 | return "<unknown>"; |
71 | } |
84 | } |
72 | |
85 | |
73 | configuration::configuration () |
86 | void |
|
|
87 | conf_node::print () |
74 | { |
88 | { |
75 | init (); |
89 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
|
|
90 | id, |
|
|
91 | id >> 8, id & 0xff, |
|
|
92 | compress ? 'Y' : 'N', |
|
|
93 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
94 | connectmode == C_NEVER ? "never" : |
|
|
95 | connectmode == C_ALWAYS ? "always" : "", |
|
|
96 | nodename, |
|
|
97 | hostname ? hostname : "", |
|
|
98 | hostname ? ":" : "", |
|
|
99 | hostname ? udp_port : 0 |
|
|
100 | ); |
76 | } |
101 | } |
77 | |
102 | |
78 | configuration::~configuration () |
103 | conf_node::~conf_node () |
79 | { |
104 | { |
80 | cleanup (); |
105 | if (rsa_key) |
|
|
106 | RSA_free (rsa_key); |
|
|
107 | |
|
|
108 | free (nodename); |
|
|
109 | free (hostname); |
81 | } |
110 | } |
82 | |
111 | |
83 | void configuration::init () |
112 | void configuration::init () |
84 | { |
113 | { |
85 | memset (this, 0, sizeof (*this)); |
114 | memset (this, 0, sizeof (*this)); |
86 | |
115 | |
87 | rekey = DEFAULT_REKEY; |
116 | rekey = DEFAULT_REKEY; |
88 | keepalive = DEFAULT_KEEPALIVE; |
117 | keepalive = DEFAULT_KEEPALIVE; |
89 | llevel = L_INFO; |
118 | llevel = L_INFO; |
90 | ip_proto = IPPROTO_GRE; |
119 | ip_proto = IPPROTO_GRE; |
|
|
120 | #if ENABLE_ICMP |
|
|
121 | icmp_type = ICMP_ECHOREPLY; |
|
|
122 | #endif |
91 | |
123 | |
92 | default_node.udp_port = DEFAULT_UDPPORT; |
124 | default_node.udp_port = DEFAULT_UDPPORT; |
93 | default_node.tcp_port = DEFAULT_UDPPORT; |
125 | default_node.tcp_port = DEFAULT_UDPPORT; |
94 | default_node.connectmode = conf_node::C_ALWAYS; |
126 | default_node.connectmode = conf_node::C_ALWAYS; |
95 | default_node.compress = true; |
127 | default_node.compress = true; |
… | |
… | |
99 | void configuration::cleanup() |
131 | void configuration::cleanup() |
100 | { |
132 | { |
101 | if (rsa_key) |
133 | if (rsa_key) |
102 | RSA_free (rsa_key); |
134 | RSA_free (rsa_key); |
103 | |
135 | |
104 | free (ifname); |
|
|
105 | |
|
|
106 | rsa_key = 0; |
136 | rsa_key = 0; |
107 | ifname = 0; |
137 | |
|
|
138 | free (ifname); ifname = 0; |
|
|
139 | #if ENABLE_HTTP_PROXY |
|
|
140 | free (proxy_host); proxy_host = 0; |
|
|
141 | free (proxy_auth); proxy_auth = 0; |
|
|
142 | #endif |
108 | } |
143 | } |
109 | |
144 | |
110 | void |
145 | void |
111 | configuration::clear_config () |
146 | configuration::clear_config () |
112 | { |
147 | { |
… | |
… | |
203 | else |
238 | else |
204 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
239 | slog (L_WARN, "'%s': %s, at '%s' line %d", val, UNKNOWN_LOGLEVEL, fname, line); |
205 | } |
240 | } |
206 | else if (!strcmp (var, "ip-proto")) |
241 | else if (!strcmp (var, "ip-proto")) |
207 | ip_proto = atoi (val); |
242 | ip_proto = atoi (val); |
|
|
243 | #if ENABLE_ICMP |
|
|
244 | //TODO: error message |
|
|
245 | else if (!strcmp (var, "icmp-type")) |
|
|
246 | icmp_type = atoi (val); |
|
|
247 | #endif |
208 | |
248 | |
209 | // per config |
249 | // per config |
210 | else if (!strcmp (var, "node")) |
250 | else if (!strcmp (var, "node")) |
211 | { |
251 | { |
212 | default_node.id++; |
252 | default_node.id++; |
… | |
… | |
271 | script_if_up = strdup (val); |
311 | script_if_up = strdup (val); |
272 | else if (!strcmp (var, "node-up")) |
312 | else if (!strcmp (var, "node-up")) |
273 | script_node_up = strdup (val); |
313 | script_node_up = strdup (val); |
274 | else if (!strcmp (var, "node-down")) |
314 | else if (!strcmp (var, "node-down")) |
275 | script_node_down = strdup (val); |
315 | script_node_down = strdup (val); |
|
|
316 | #if ENABLE_HTTP_PROXY |
|
|
317 | else if (!strcmp (var, "http-proxy-host")) |
|
|
318 | proxy_host = strdup (val); |
|
|
319 | else if (!strcmp (var, "http-proxy-port")) |
|
|
320 | proxy_port = atoi (val); |
|
|
321 | else if (!strcmp (var, "http-proxy-auth")) |
|
|
322 | proxy_auth = (char *)base64_encode ((const u8 *)val, strlen (val)); |
|
|
323 | #endif |
276 | |
324 | |
277 | /* node-specific, non-defaultable */ |
325 | /* node-specific, non-defaultable */ |
278 | else if (node != &default_node && !strcmp (var, "hostname")) |
326 | else if (node != &default_node && !strcmp (var, "hostname")) |
279 | { |
327 | { |
280 | free (node->hostname); |
328 | free (node->hostname); |
… | |
… | |
312 | parse_bool (node->compress, "compress", true, false); |
360 | parse_bool (node->compress, "compress", true, false); |
313 | } |
361 | } |
314 | // all these bool options really really cost a lot of executable size! |
362 | // all these bool options really really cost a lot of executable size! |
315 | else if (!strcmp (var, "enable-tcp")) |
363 | else if (!strcmp (var, "enable-tcp")) |
316 | { |
364 | { |
|
|
365 | #if ENABLE_TCP |
317 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
366 | u8 v; parse_bool (v, "enable-tcp" , PROT_TCPv4, 0); node->protocols = (node->protocols & ~PROT_TCPv4) | v; |
|
|
367 | #endif |
|
|
368 | } |
|
|
369 | else if (!strcmp (var, "enable-icmp")) |
|
|
370 | { |
|
|
371 | #if ENABLE_ICMP |
|
|
372 | u8 v; parse_bool (v, "enable-icmp" , PROT_ICMPv4, 0); node->protocols = (node->protocols & ~PROT_ICMPv4) | v; |
|
|
373 | #endif |
318 | } |
374 | } |
319 | else if (!strcmp (var, "enable-udp")) |
375 | else if (!strcmp (var, "enable-udp")) |
320 | { |
376 | { |
321 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
377 | u8 v; parse_bool (v, "enable-udp" , PROT_UDPv4, 0); node->protocols = (node->protocols & ~PROT_UDPv4) | v; |
322 | } |
378 | } |
… | |
… | |
325 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
381 | u8 v; parse_bool (v, "enable-rawip", PROT_IPv4, 0); node->protocols = (node->protocols & ~PROT_IPv4 ) | v; |
326 | } |
382 | } |
327 | |
383 | |
328 | // unknown or misplaced |
384 | // unknown or misplaced |
329 | else |
385 | else |
330 | { |
|
|
331 | slog (L_WARN, |
386 | slog (L_WARN, |
332 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
387 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
333 | var, fname, lineno); |
388 | var, fname, lineno); |
334 | } |
|
|
335 | } |
389 | } |
336 | |
390 | |
337 | fclose (f); |
391 | fclose (f); |
338 | } |
392 | } |
339 | else |
393 | else |
… | |
… | |
398 | printf (_("MTU: %d\n"), mtu); |
452 | printf (_("MTU: %d\n"), mtu); |
399 | printf (_("rekeying interval: %d\n"), rekey); |
453 | printf (_("rekeying interval: %d\n"), rekey); |
400 | printf (_("keepalive interval: %d\n"), keepalive); |
454 | printf (_("keepalive interval: %d\n"), keepalive); |
401 | printf (_("interface: %s\n"), ifname); |
455 | printf (_("interface: %s\n"), ifname); |
402 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
456 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
403 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) : -1); |
457 | printf (_("rsa key size: %d\n"), rsa_key ? RSA_size (rsa_key) * 8 : -1); |
404 | printf ("\n"); |
458 | printf ("\n"); |
405 | |
459 | |
406 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
460 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
407 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
461 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
408 | |
462 | |
… | |
… | |
410 | (*i)->print (); |
464 | (*i)->print (); |
411 | |
465 | |
412 | printf ("\n"); |
466 | printf ("\n"); |
413 | } |
467 | } |
414 | |
468 | |
415 | void |
469 | configuration::configuration () |
416 | conf_node::print () |
|
|
417 | { |
470 | { |
418 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
471 | init (); |
419 | id, |
|
|
420 | id >> 8, id & 0xff, |
|
|
421 | compress ? 'Y' : 'N', |
|
|
422 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
423 | connectmode == C_NEVER ? "never" : |
|
|
424 | connectmode == C_ALWAYS ? "always" : "", |
|
|
425 | nodename, |
|
|
426 | hostname ? hostname : "", |
|
|
427 | hostname ? ":" : "", |
|
|
428 | hostname ? udp_port : 0 |
|
|
429 | ); |
|
|
430 | } |
472 | } |
431 | |
473 | |
|
|
474 | configuration::~configuration () |
|
|
475 | { |
|
|
476 | cleanup (); |
|
|
477 | } |
|
|
478 | |
|
|
479 | |