… | |
… | |
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> |
… | |
… | |
51 | char *identname; |
61 | char *identname; |
52 | char *pidfilename; |
62 | char *pidfilename; |
53 | |
63 | |
54 | struct configuration conf; |
64 | struct configuration conf; |
55 | |
65 | |
56 | configuration::configuration () |
66 | u8 best_protocol (u8 protset) |
57 | { |
67 | { |
58 | init (); |
68 | if (protset & PROT_IPv4 ) return PROT_IPv4; |
59 | } |
69 | if (protset & PROT_ICMPv4) return PROT_ICMPv4; |
|
|
70 | if (protset & PROT_UDPv4 ) return PROT_UDPv4; |
|
|
71 | if (protset & PROT_TCPv4 ) return PROT_TCPv4; |
60 | |
72 | |
61 | configuration::~configuration () |
73 | return 0; |
|
|
74 | } |
|
|
75 | |
|
|
76 | const char *strprotocol (u8 protocol) |
62 | { |
77 | { |
63 | cleanup (); |
78 | if (protocol & PROT_IPv4 ) return "rawip"; |
|
|
79 | if (protocol & PROT_ICMPv4) return "icmp"; |
|
|
80 | if (protocol & PROT_UDPv4 ) return "udp"; |
|
|
81 | if (protocol & PROT_TCPv4 ) return "tcp"; |
|
|
82 | |
|
|
83 | return "<unknown>"; |
|
|
84 | } |
|
|
85 | |
|
|
86 | void |
|
|
87 | conf_node::print () |
|
|
88 | { |
|
|
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 | ); |
|
|
101 | } |
|
|
102 | |
|
|
103 | conf_node::~conf_node () |
|
|
104 | { |
|
|
105 | if (rsa_key) |
|
|
106 | RSA_free (rsa_key); |
|
|
107 | |
|
|
108 | free (nodename); |
|
|
109 | free (hostname); |
64 | } |
110 | } |
65 | |
111 | |
66 | void configuration::init () |
112 | void configuration::init () |
67 | { |
113 | { |
68 | memset (this, 0, sizeof (*this)); |
114 | memset (this, 0, sizeof (*this)); |
69 | |
115 | |
70 | rekey = DEFAULT_REKEY; |
116 | rekey = DEFAULT_REKEY; |
71 | keepalive = DEFAULT_KEEPALIVE; |
117 | keepalive = DEFAULT_KEEPALIVE; |
72 | llevel = L_INFO; |
118 | llevel = L_INFO; |
73 | ip_proto = IPPROTO_GRE; |
119 | ip_proto = IPPROTO_GRE; |
|
|
120 | #if ENABLE_ICMP |
|
|
121 | icmp_type = ICMP_ECHOREPLY; |
|
|
122 | #endif |
74 | |
123 | |
75 | default_node.udp_port = DEFAULT_UDPPORT; |
124 | default_node.udp_port = DEFAULT_UDPPORT; |
|
|
125 | default_node.tcp_port = DEFAULT_UDPPORT; |
76 | default_node.connectmode = conf_node::C_ALWAYS; |
126 | default_node.connectmode = conf_node::C_ALWAYS; |
77 | default_node.compress = true; |
127 | default_node.compress = true; |
78 | default_node.protocols = PROT_UDPv4; |
128 | default_node.protocols = PROT_UDPv4; |
79 | } |
129 | } |
80 | |
130 | |
81 | void configuration::cleanup() |
131 | void configuration::cleanup() |
82 | { |
132 | { |
83 | if (rsa_key) |
133 | if (rsa_key) |
84 | RSA_free (rsa_key); |
134 | RSA_free (rsa_key); |
85 | |
135 | |
86 | free (ifname); |
|
|
87 | |
|
|
88 | rsa_key = 0; |
136 | rsa_key = 0; |
89 | 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 |
90 | } |
143 | } |
91 | |
144 | |
92 | void |
145 | void |
93 | configuration::clear_config () |
146 | configuration::clear_config () |
94 | { |
147 | { |
… | |
… | |
185 | else |
238 | else |
186 | 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); |
187 | } |
240 | } |
188 | else if (!strcmp (var, "ip-proto")) |
241 | else if (!strcmp (var, "ip-proto")) |
189 | 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 |
190 | |
248 | |
191 | // per config |
249 | // per config |
192 | else if (!strcmp (var, "node")) |
250 | else if (!strcmp (var, "node")) |
193 | { |
251 | { |
194 | default_node.id++; |
252 | default_node.id++; |
… | |
… | |
253 | script_if_up = strdup (val); |
311 | script_if_up = strdup (val); |
254 | else if (!strcmp (var, "node-up")) |
312 | else if (!strcmp (var, "node-up")) |
255 | script_node_up = strdup (val); |
313 | script_node_up = strdup (val); |
256 | else if (!strcmp (var, "node-down")) |
314 | else if (!strcmp (var, "node-down")) |
257 | 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 |
258 | |
324 | |
259 | /* node-specific, non-defaultable */ |
325 | /* node-specific, non-defaultable */ |
260 | else if (node != &default_node && !strcmp (var, "hostname")) |
326 | else if (node != &default_node && !strcmp (var, "hostname")) |
261 | { |
327 | { |
262 | free (node->hostname); |
328 | free (node->hostname); |
… | |
… | |
264 | } |
330 | } |
265 | |
331 | |
266 | /* node-specific, defaultable */ |
332 | /* node-specific, defaultable */ |
267 | else if (!strcmp (var, "udp-port")) |
333 | else if (!strcmp (var, "udp-port")) |
268 | node->udp_port = atoi (val); |
334 | node->udp_port = atoi (val); |
|
|
335 | else if (!strcmp (var, "tcp-port")) |
|
|
336 | node->tcp_port = atoi (val); |
269 | else if (!strcmp (var, "router-priority")) |
337 | else if (!strcmp (var, "router-priority")) |
270 | node->routerprio = atoi (val); |
338 | node->routerprio = atoi (val); |
271 | else if (!strcmp (var, "connect")) |
339 | else if (!strcmp (var, "connect")) |
272 | { |
340 | { |
273 | if (!strcmp (val, "ondemand")) |
341 | if (!strcmp (val, "ondemand")) |
… | |
… | |
290 | else if (!strcmp (var, "compress")) |
358 | else if (!strcmp (var, "compress")) |
291 | { |
359 | { |
292 | parse_bool (node->compress, "compress", true, false); |
360 | parse_bool (node->compress, "compress", true, false); |
293 | } |
361 | } |
294 | // 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! |
|
|
363 | else if (!strcmp (var, "enable-tcp")) |
|
|
364 | { |
|
|
365 | #if ENABLE_TCP |
|
|
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 |
|
|
374 | } |
295 | else if (!strcmp (var, "enable-udp")) |
375 | else if (!strcmp (var, "enable-udp")) |
296 | { |
376 | { |
297 | 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; |
298 | } |
378 | } |
299 | else if (!strcmp (var, "enable-rawip")) |
379 | else if (!strcmp (var, "enable-rawip")) |
… | |
… | |
301 | 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; |
302 | } |
382 | } |
303 | |
383 | |
304 | // unknown or misplaced |
384 | // unknown or misplaced |
305 | else |
385 | else |
306 | { |
|
|
307 | slog (L_WARN, |
386 | slog (L_WARN, |
308 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
387 | _("unknown or misplaced variable `%s', at '%s' line %d"), |
309 | var, fname, lineno); |
388 | var, fname, lineno); |
310 | } |
|
|
311 | } |
389 | } |
312 | |
390 | |
313 | fclose (f); |
391 | fclose (f); |
314 | } |
392 | } |
315 | else |
393 | else |
… | |
… | |
374 | printf (_("MTU: %d\n"), mtu); |
452 | printf (_("MTU: %d\n"), mtu); |
375 | printf (_("rekeying interval: %d\n"), rekey); |
453 | printf (_("rekeying interval: %d\n"), rekey); |
376 | printf (_("keepalive interval: %d\n"), keepalive); |
454 | printf (_("keepalive interval: %d\n"), keepalive); |
377 | printf (_("interface: %s\n"), ifname); |
455 | printf (_("interface: %s\n"), ifname); |
378 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
456 | printf (_("primary rsa key: %s\n"), prikeyfile ? prikeyfile : "<default>"); |
379 | 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); |
380 | printf ("\n"); |
458 | printf ("\n"); |
381 | |
459 | |
382 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
460 | printf ("%4s %-17s %s %-8.8s %-10.10s %s\n", |
383 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
461 | _("ID#"), _("MAC"), _("Com"), _("Conmode"), _("Node"), _("Host:Port")); |
384 | |
462 | |
… | |
… | |
386 | (*i)->print (); |
464 | (*i)->print (); |
387 | |
465 | |
388 | printf ("\n"); |
466 | printf ("\n"); |
389 | } |
467 | } |
390 | |
468 | |
391 | void |
469 | configuration::configuration () |
392 | conf_node::print () |
|
|
393 | { |
470 | { |
394 | printf ("%4d fe:fd:80:00:0%1x:%02x %c %-8.8s %-10.10s %s%s%d\n", |
471 | init (); |
395 | id, |
|
|
396 | id >> 8, id & 0xff, |
|
|
397 | compress ? 'Y' : 'N', |
|
|
398 | connectmode == C_ONDEMAND ? "ondemand" : |
|
|
399 | connectmode == C_NEVER ? "never" : |
|
|
400 | connectmode == C_ALWAYS ? "always" : "", |
|
|
401 | nodename, |
|
|
402 | hostname ? hostname : "", |
|
|
403 | hostname ? ":" : "", |
|
|
404 | hostname ? udp_port : 0 |
|
|
405 | ); |
|
|
406 | } |
472 | } |
407 | |
473 | |
|
|
474 | configuration::~configuration () |
|
|
475 | { |
|
|
476 | cleanup (); |
|
|
477 | } |
|
|
478 | |
|
|
479 | |