d292d850ced2f43d3f187ace41d28a953b6e47fb
[tinc] / src / net_setup.c
1 /*
2     net_setup.c -- Setup.
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5                   2006      Scott Lamb <slamb@slamb.org>
6                   2010      Brandon Black <blblack@gmail.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "system.h"
24
25 #include "splay_tree.h"
26 #include "cipher.h"
27 #include "conf.h"
28 #include "connection.h"
29 #include "control.h"
30 #include "device.h"
31 #include "digest.h"
32 #include "ecdsa.h"
33 #include "graph.h"
34 #include "logger.h"
35 #include "net.h"
36 #include "netutl.h"
37 #include "process.h"
38 #include "protocol.h"
39 #include "route.h"
40 #include "rsa.h"
41 #include "subnet.h"
42 #include "utils.h"
43 #include "xalloc.h"
44
45 char *myport;
46 static struct event device_ev;
47 devops_t devops;
48
49 bool node_read_ecdsa_public_key(node_t *n) {
50         if(ecdsa_active(&n->ecdsa))
51                 return true;
52
53         splay_tree_t *config_tree;
54         FILE *fp;
55         char *fname;
56         char *p;
57         bool result = false;
58
59         xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
60
61         init_configuration(&config_tree);
62         if(!read_config_file(config_tree, fname))
63                 goto exit;
64
65         /* First, check for simple ECDSAPublicKey statement */
66
67         if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
68                 result = ecdsa_set_base64_public_key(&n->ecdsa, p);
69                 free(p);
70                 goto exit;
71         }
72
73         /* Else, check for ECDSAPublicKeyFile statement and read it */
74
75         free(fname);
76
77         if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &fname))
78                 xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
79
80         fp = fopen(fname, "r");
81
82         if(!fp) {
83                 logger(LOG_ERR, "Error reading ECDSA public key file `%s': %s", fname, strerror(errno));
84                 goto exit;
85         }
86
87         result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
88         fclose(fp);
89
90 exit:
91         exit_configuration(&config_tree);
92         free(fname);
93         return result;
94 }
95
96 bool read_ecdsa_public_key(connection_t *c) {
97         FILE *fp;
98         char *fname;
99         char *p;
100         bool result;
101
102         /* First, check for simple ECDSAPublicKey statement */
103
104         if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
105                 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
106                 free(p);
107                 return result;
108         }
109
110         /* Else, check for ECDSAPublicKeyFile statement and read it */
111
112         if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
113                 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
114
115         fp = fopen(fname, "r");
116
117         if(!fp) {
118                 logger(LOG_ERR, "Error reading ECDSA public key file `%s': %s",
119                            fname, strerror(errno));
120                 free(fname);
121                 return false;
122         }
123
124         result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
125         fclose(fp);
126
127         if(!result) 
128                 logger(LOG_ERR, "Reading ECDSA public key file `%s' failed: %s", fname, strerror(errno));
129         free(fname);
130         return result;
131 }
132
133 bool read_rsa_public_key(connection_t *c) {
134         FILE *fp;
135         char *fname;
136         char *n;
137         bool result;
138
139         /* First, check for simple PublicKey statement */
140
141         if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
142                 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
143                 free(n);
144                 return result;
145         }
146
147         /* Else, check for PublicKeyFile statement and read it */
148
149         if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
150                 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
151
152         fp = fopen(fname, "r");
153
154         if(!fp) {
155                 logger(LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
156                 free(fname);
157                 return false;
158         }
159
160         result = rsa_read_pem_public_key(&c->rsa, fp);
161         fclose(fp);
162
163         if(!result) 
164                 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
165         free(fname);
166         return result;
167 }
168
169 static bool read_ecdsa_private_key(void) {
170         FILE *fp;
171         char *fname;
172         bool result;
173
174         /* Check for PrivateKeyFile statement and read it */
175
176         if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
177                 xasprintf(&fname, "%s/ecdsa_key.priv", confbase);
178
179         fp = fopen(fname, "r");
180
181         if(!fp) {
182                 logger(LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
183                 free(fname);
184                 return false;
185         }
186
187 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
188         struct stat s;
189
190         if(fstat(fileno(fp), &s)) {
191                 logger(LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
192                 free(fname);
193                 return false;
194         }
195
196         if(s.st_mode & ~0100700)
197                 logger(LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
198 #endif
199
200         result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
201         fclose(fp);
202
203         if(!result) 
204                 logger(LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
205         free(fname);
206         return result;
207 }
208
209 static bool read_rsa_private_key(void) {
210         FILE *fp;
211         char *fname;
212         char *n, *d;
213         bool result;
214
215         /* First, check for simple PrivateKey statement */
216
217         if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
218                 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
219                         logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
220                         free(d);
221                         return false;
222                 }
223                 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
224                 free(n);
225                 free(d);
226                 return true;
227         }
228
229         /* Else, check for PrivateKeyFile statement and read it */
230
231         if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
232                 xasprintf(&fname, "%s/rsa_key.priv", confbase);
233
234         fp = fopen(fname, "r");
235
236         if(!fp) {
237                 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
238                            fname, strerror(errno));
239                 free(fname);
240                 return false;
241         }
242
243 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
244         struct stat s;
245
246         if(fstat(fileno(fp), &s)) {
247                 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
248                 free(fname);
249                 return false;
250         }
251
252         if(s.st_mode & ~0100700)
253                 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
254 #endif
255
256         result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
257         fclose(fp);
258
259         if(!result) 
260                 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
261         free(fname);
262         return result;
263 }
264
265 static struct event keyexpire_event;
266
267 static void keyexpire_handler(int fd, short events, void *data) {
268         regenerate_key();
269 }
270
271 void regenerate_key(void) {
272         if(timeout_initialized(&keyexpire_event)) {
273                 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
274                 event_del(&keyexpire_event);
275                 send_key_changed();
276         } else {
277                 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
278         }
279
280         event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
281 }
282
283 /*
284   Read Subnets from all host config files
285 */
286 void load_all_subnets(void) {
287         DIR *dir;
288         struct dirent *ent;
289         char *dname;
290         char *fname;
291         splay_tree_t *config_tree;
292         config_t *cfg;
293         subnet_t *s, *s2;
294         node_t *n;
295         bool result;
296
297         xasprintf(&dname, "%s/hosts", confbase);
298         dir = opendir(dname);
299         if(!dir) {
300                 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
301                 free(dname);
302                 return;
303         }
304
305         while((ent = readdir(dir))) {
306                 if(!check_id(ent->d_name))
307                         continue;
308
309                 n = lookup_node(ent->d_name);
310                 #ifdef _DIRENT_HAVE_D_TYPE
311                 //if(ent->d_type != DT_REG)
312                 //      continue;
313                 #endif
314
315                 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
316                 init_configuration(&config_tree);
317                 result = read_config_file(config_tree, fname);
318                 free(fname);
319                 if(!result)
320                         continue;
321
322                 if(!n) {
323                         n = new_node();
324                         n->name = xstrdup(ent->d_name);
325                         node_add(n);
326                 }
327
328                 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
329                         if(!get_config_subnet(cfg, &s))
330                                 continue;
331
332                         if((s2 = lookup_subnet(n, s))) {
333                                 s2->expires = -1;
334                         } else {
335                                 subnet_add(n, s);
336                         }
337                 }
338
339                 exit_configuration(&config_tree);
340         }
341
342         closedir(dir);
343 }
344
345 /*
346   Configure node_t myself and set up the local sockets (listen only)
347 */
348 static bool setup_myself(void) {
349         config_t *cfg;
350         subnet_t *subnet;
351         char *name, *hostname, *mode, *afname, *cipher, *digest, *type;
352         char *fname = NULL;
353         char *address = NULL;
354         char *envp[5];
355         struct addrinfo *ai, *aip, hint = {0};
356         bool choice;
357         int i, err;
358         int replaywin_int;
359
360         myself = new_node();
361         myself->connection = new_connection();
362
363         myself->hostname = xstrdup("MYSELF");
364         myself->connection->hostname = xstrdup("MYSELF");
365
366         myself->connection->options = 0;
367         myself->connection->protocol_major = PROT_MAJOR;
368         myself->connection->protocol_minor = PROT_MINOR;
369
370         if(!get_config_string(lookup_config(config_tree, "Name"), &name)) {     /* Not acceptable */
371                 logger(LOG_ERR, "Name for tinc daemon required!");
372                 return false;
373         }
374
375         if(!check_id(name)) {
376                 logger(LOG_ERR, "Invalid name for myself!");
377                 free(name);
378                 return false;
379         }
380
381         myself->name = name;
382         myself->connection->name = xstrdup(name);
383         xasprintf(&fname, "%s/hosts/%s", confbase, name);
384         read_config_options(config_tree, name);
385         read_config_file(config_tree, fname);
386         free(fname);
387
388         get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
389
390         if(experimental && !read_ecdsa_private_key())
391                 return false;
392
393         if(!read_rsa_private_key())
394                 return false;
395
396         if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
397                 myport = xstrdup("655");
398
399         if(!atoi(myport)) {
400                 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
401                 sockaddr_t sa;
402                 if(!ai || !ai->ai_addr)
403                         return false;
404                 free(myport);
405                 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
406                 sockaddr2str(&sa, NULL, &myport);
407         }
408
409         /* Read in all the subnets specified in the host configuration file */
410
411         cfg = lookup_config(config_tree, "Subnet");
412
413         while(cfg) {
414                 if(!get_config_subnet(cfg, &subnet))
415                         return false;
416
417                 subnet_add(myself, subnet);
418
419                 cfg = lookup_config_next(config_tree, cfg);
420         }
421
422         /* Check some options */
423
424         if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
425                 myself->options |= OPTION_INDIRECT;
426
427         if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
428                 myself->options |= OPTION_TCPONLY;
429
430         if(myself->options & OPTION_TCPONLY)
431                 myself->options |= OPTION_INDIRECT;
432
433         get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
434         get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
435         get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
436         get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
437         strictsubnets |= tunnelserver;
438
439         if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
440                 if(!strcasecmp(mode, "router"))
441                         routing_mode = RMODE_ROUTER;
442                 else if(!strcasecmp(mode, "switch"))
443                         routing_mode = RMODE_SWITCH;
444                 else if(!strcasecmp(mode, "hub"))
445                         routing_mode = RMODE_HUB;
446                 else {
447                         logger(LOG_ERR, "Invalid routing mode!");
448                         return false;
449                 }
450                 free(mode);
451         }
452
453         if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
454                 if(!strcasecmp(mode, "off"))
455                         forwarding_mode = FMODE_OFF;
456                 else if(!strcasecmp(mode, "internal"))
457                         forwarding_mode = FMODE_INTERNAL;
458                 else if(!strcasecmp(mode, "kernel"))
459                         forwarding_mode = FMODE_KERNEL;
460                 else {
461                         logger(LOG_ERR, "Invalid forwarding mode!");
462                         return false;
463                 }
464                 free(mode);
465         }
466
467         choice = true;
468         get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
469         if(choice)
470                 myself->options |= OPTION_PMTU_DISCOVERY;
471
472         choice = true;
473         get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
474         if(choice)
475                 myself->options |= OPTION_CLAMP_MSS;
476
477         get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
478         get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
479         get_config_bool(lookup_config(config_tree, "Broadcast"), &broadcast);
480
481 #if !defined(SOL_IP) || !defined(IP_TOS)
482         if(priorityinheritance)
483                 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
484 #endif
485
486         if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
487                 macexpire = 600;
488
489         if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
490                 if(maxtimeout <= 0) {
491                         logger(LOG_ERR, "Bogus maximum timeout!");
492                         return false;
493                 }
494         } else
495                 maxtimeout = 900;
496
497         if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
498                 if(udp_rcvbuf <= 0) {
499                         logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
500                         return false;
501                 }
502         }
503
504         if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
505                 if(udp_sndbuf <= 0) {
506                         logger(LOG_ERR, "UDPSndBuf cannot be negative!");
507                         return false;
508                 }
509         }
510
511         if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
512                 if(replaywin_int < 0) {
513                         logger(LOG_ERR, "ReplayWindow cannot be negative!");
514                         return false;
515                 }
516                 replaywin = (unsigned)replaywin_int;
517         }
518
519         if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
520                 if(!strcasecmp(afname, "IPv4"))
521                         addressfamily = AF_INET;
522                 else if(!strcasecmp(afname, "IPv6"))
523                         addressfamily = AF_INET6;
524                 else if(!strcasecmp(afname, "any"))
525                         addressfamily = AF_UNSPEC;
526                 else {
527                         logger(LOG_ERR, "Invalid address family!");
528                         return false;
529                 }
530                 free(afname);
531         }
532
533         get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
534
535         /* Generate packet encryption key */
536
537         if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
538                 cipher = xstrdup("blowfish");
539
540         if(!cipher_open_by_name(&myself->incipher, cipher)) {
541                 logger(LOG_ERR, "Unrecognized cipher type!");
542                 return false;
543         }
544
545         if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
546                 keylifetime = 3600;
547
548         regenerate_key();
549
550         /* Check if we want to use message authentication codes... */
551
552         int maclength = 4;
553         get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
554
555         if(maclength < 0) {
556                 logger(LOG_ERR, "Bogus MAC length!");
557                 return false;
558         }
559
560         if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
561                 digest = xstrdup("sha1");
562
563         if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
564                 logger(LOG_ERR, "Unrecognized digest type!");
565                 return false;
566         }
567
568         /* Compression */
569
570         if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
571                 if(myself->incompression < 0 || myself->incompression > 11) {
572                         logger(LOG_ERR, "Bogus compression level!");
573                         return false;
574                 }
575         } else
576                 myself->incompression = 0;
577
578         myself->connection->outcompression = 0;
579
580         /* Done */
581
582         myself->nexthop = myself;
583         myself->via = myself;
584         myself->status.reachable = true;
585         node_add(myself);
586
587         graph();
588
589         if(strictsubnets)
590                 load_all_subnets();
591
592         /* Open device */
593
594         devops = os_devops;
595
596         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
597                 if(!strcasecmp(type, "dummy"))
598                         devops = dummy_devops;
599                 else if(!strcasecmp(type, "raw_socket"))
600                         devops = raw_socket_devops;
601 #ifdef ENABLE_UML
602                 else if(!strcasecmp(type, "uml"))
603                         devops = uml_devops;
604 #endif
605 #ifdef ENABLE_VDE
606                 else if(!strcasecmp(type, "vde"))
607                         devops = vde_devops;
608 #endif
609         }
610
611         if(!devops.setup())
612                 return false;
613
614         if(device_fd >= 0) {
615                 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
616
617                 if (event_add(&device_ev, NULL) < 0) {
618                         logger(LOG_ERR, "event_add failed: %s", strerror(errno));
619                         devops.close();
620                         return false;
621                 }
622         }
623
624         /* Run tinc-up script to further initialize the tap interface */
625         xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
626         xasprintf(&envp[1], "DEVICE=%s", device ? : "");
627         xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
628         xasprintf(&envp[3], "NAME=%s", myself->name);
629         envp[4] = NULL;
630
631         execute_script("tinc-up", envp);
632
633         for(i = 0; i < 4; i++)
634                 free(envp[i]);
635
636         /* Run subnet-up scripts for our own subnets */
637
638         subnet_update(myself, NULL, true);
639
640         /* Open sockets */
641
642         listen_sockets = 0;
643         cfg = lookup_config(config_tree, "BindToAddress");
644
645         do {
646                 get_config_string(cfg, &address);
647                 if(cfg)
648                         cfg = lookup_config_next(config_tree, cfg);
649
650                 hint.ai_family = addressfamily;
651                 hint.ai_socktype = SOCK_STREAM;
652                 hint.ai_protocol = IPPROTO_TCP;
653                 hint.ai_flags = AI_PASSIVE;
654
655                 err = getaddrinfo(address, myport, &hint, &ai);
656                 free(address);
657
658                 if(err || !ai) {
659                         logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
660                                    gai_strerror(err));
661                         return false;
662                 }
663
664                 for(aip = ai; aip; aip = aip->ai_next) {
665                         if(listen_sockets >= MAXSOCKETS) {
666                                 logger(LOG_ERR, "Too many listening sockets");
667                                 return false;
668                         }
669
670                         listen_socket[listen_sockets].tcp =
671                                 setup_listen_socket((sockaddr_t *) aip->ai_addr);
672
673                         if(listen_socket[listen_sockets].tcp < 0)
674                                 continue;
675
676                         listen_socket[listen_sockets].udp =
677                                 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
678
679                         if(listen_socket[listen_sockets].udp < 0) {
680                                 close(listen_socket[listen_sockets].tcp);
681                                 continue;
682                         }
683
684                         event_set(&listen_socket[listen_sockets].ev_tcp,
685                                           listen_socket[listen_sockets].tcp,
686                                           EV_READ|EV_PERSIST,
687                                           handle_new_meta_connection, NULL);
688                         if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
689                                 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
690                                 abort();
691                         }
692
693                         event_set(&listen_socket[listen_sockets].ev_udp,
694                                           listen_socket[listen_sockets].udp,
695                                           EV_READ|EV_PERSIST,
696                                           handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
697                         if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
698                                 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
699                                 abort();
700                         }
701
702                         ifdebug(CONNECTIONS) {
703                                 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
704                                 logger(LOG_NOTICE, "Listening on %s", hostname);
705                                 free(hostname);
706                         }
707
708                         memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
709                         listen_sockets++;
710                 }
711
712                 freeaddrinfo(ai);
713         } while(cfg);
714
715         if(listen_sockets)
716                 logger(LOG_NOTICE, "Ready");
717         else {
718                 logger(LOG_ERR, "Unable to create any listening socket!");
719                 return false;
720         }
721
722         return true;
723 }
724
725 /*
726   initialize network
727 */
728 bool setup_network(void) {
729         init_connections();
730         init_subnets();
731         init_nodes();
732         init_edges();
733         init_requests();
734
735         if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
736                 if(pinginterval < 1) {
737                         pinginterval = 86400;
738                 }
739         } else
740                 pinginterval = 60;
741
742         if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
743                 pingtimeout = 5;
744         if(pingtimeout < 1 || pingtimeout > pinginterval)
745                 pingtimeout = pinginterval;
746
747         if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
748                 maxoutbufsize = 10 * MTU;
749
750         if(!setup_myself())
751                 return false;
752
753         return true;
754 }
755
756 /*
757   close all open network connections
758 */
759 void close_network_connections(void) {
760         splay_node_t *node, *next;
761         connection_t *c;
762         char *envp[5];
763         int i;
764
765         for(node = connection_tree->head; node; node = next) {
766                 next = node->next;
767                 c = node->data;
768                 c->outgoing = NULL;
769                 terminate_connection(c, false);
770         }
771
772         list_delete_list(outgoing_list);
773
774         if(myself && myself->connection) {
775                 subnet_update(myself, NULL, false);
776                 terminate_connection(myself->connection, false);
777                 free_connection(myself->connection);
778         }
779
780         for(i = 0; i < listen_sockets; i++) {
781                 event_del(&listen_socket[i].ev_tcp);
782                 event_del(&listen_socket[i].ev_udp);
783                 close(listen_socket[i].tcp);
784                 close(listen_socket[i].udp);
785         }
786
787         xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
788         xasprintf(&envp[1], "DEVICE=%s", device ? : "");
789         xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
790         xasprintf(&envp[3], "NAME=%s", myself->name);
791         envp[4] = NULL;
792
793         exit_requests();
794         exit_edges();
795         exit_subnets();
796         exit_nodes();
797         exit_connections();
798
799         execute_script("tinc-down", envp);
800
801         if(myport) free(myport);
802
803         for(i = 0; i < 4; i++)
804                 free(envp[i]);
805
806         devops.close();
807
808         return;
809 }