]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
Merge branch 'master' of git://tinc-vpn.org/tinc into 1.1
[meshlink] / 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         strictsubnets |= tunnelserver;
437
438         if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
439                 if(!strcasecmp(mode, "router"))
440                         routing_mode = RMODE_ROUTER;
441                 else if(!strcasecmp(mode, "switch"))
442                         routing_mode = RMODE_SWITCH;
443                 else if(!strcasecmp(mode, "hub"))
444                         routing_mode = RMODE_HUB;
445                 else {
446                         logger(LOG_ERR, "Invalid routing mode!");
447                         return false;
448                 }
449                 free(mode);
450         }
451
452         if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
453                 if(!strcasecmp(mode, "off"))
454                         forwarding_mode = FMODE_OFF;
455                 else if(!strcasecmp(mode, "internal"))
456                         forwarding_mode = FMODE_INTERNAL;
457                 else if(!strcasecmp(mode, "kernel"))
458                         forwarding_mode = FMODE_KERNEL;
459                 else {
460                         logger(LOG_ERR, "Invalid forwarding mode!");
461                         return false;
462                 }
463                 free(mode);
464         }
465
466         choice = true;
467         get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
468         if(choice)
469                 myself->options |= OPTION_PMTU_DISCOVERY;
470
471         choice = true;
472         get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
473         if(choice)
474                 myself->options |= OPTION_CLAMP_MSS;
475
476         get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
477         get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
478         get_config_bool(lookup_config(config_tree, "Broadcast"), &broadcast);
479
480 #if !defined(SOL_IP) || !defined(IP_TOS)
481         if(priorityinheritance)
482                 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
483 #endif
484
485         if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
486                 macexpire = 600;
487
488         if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
489                 if(maxtimeout <= 0) {
490                         logger(LOG_ERR, "Bogus maximum timeout!");
491                         return false;
492                 }
493         } else
494                 maxtimeout = 900;
495
496         if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
497                 if(udp_rcvbuf <= 0) {
498                         logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
499                         return false;
500                 }
501         }
502
503         if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
504                 if(udp_sndbuf <= 0) {
505                         logger(LOG_ERR, "UDPSndBuf cannot be negative!");
506                         return false;
507                 }
508         }
509
510         if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
511                 if(replaywin_int < 0) {
512                         logger(LOG_ERR, "ReplayWindow cannot be negative!");
513                         return false;
514                 }
515                 replaywin = (unsigned)replaywin_int;
516         }
517
518         if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
519                 if(!strcasecmp(afname, "IPv4"))
520                         addressfamily = AF_INET;
521                 else if(!strcasecmp(afname, "IPv6"))
522                         addressfamily = AF_INET6;
523                 else if(!strcasecmp(afname, "any"))
524                         addressfamily = AF_UNSPEC;
525                 else {
526                         logger(LOG_ERR, "Invalid address family!");
527                         return false;
528                 }
529                 free(afname);
530         }
531
532         get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
533
534         /* Generate packet encryption key */
535
536         if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
537                 cipher = xstrdup("blowfish");
538
539         if(!cipher_open_by_name(&myself->incipher, cipher)) {
540                 logger(LOG_ERR, "Unrecognized cipher type!");
541                 return false;
542         }
543
544         if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
545                 keylifetime = 3600;
546
547         regenerate_key();
548
549         /* Check if we want to use message authentication codes... */
550
551         int maclength = 4;
552         get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
553
554         if(maclength < 0) {
555                 logger(LOG_ERR, "Bogus MAC length!");
556                 return false;
557         }
558
559         if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
560                 digest = xstrdup("sha1");
561
562         if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
563                 logger(LOG_ERR, "Unrecognized digest type!");
564                 return false;
565         }
566
567         /* Compression */
568
569         if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
570                 if(myself->incompression < 0 || myself->incompression > 11) {
571                         logger(LOG_ERR, "Bogus compression level!");
572                         return false;
573                 }
574         } else
575                 myself->incompression = 0;
576
577         myself->connection->outcompression = 0;
578
579         /* Done */
580
581         myself->nexthop = myself;
582         myself->via = myself;
583         myself->status.reachable = true;
584         node_add(myself);
585
586         graph();
587
588         if(strictsubnets)
589                 load_all_subnets();
590
591         /* Open device */
592
593         devops = os_devops;
594
595         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
596                 if(!strcasecmp(type, "dummy"))
597                         devops = dummy_devops;
598                 else if(!strcasecmp(type, "raw_socket"))
599                         devops = raw_socket_devops;
600 #ifdef ENABLE_UML
601                 else if(!strcasecmp(type, "uml"))
602                         devops = uml_devops;
603 #endif
604 #ifdef ENABLE_VDE
605                 else if(!strcasecmp(type, "vde"))
606                         devops = vde_devops;
607 #endif
608         }
609
610         if(!devops.setup())
611                 return false;
612
613         if(device_fd >= 0) {
614                 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
615
616                 if (event_add(&device_ev, NULL) < 0) {
617                         logger(LOG_ERR, "event_add failed: %s", strerror(errno));
618                         devops.close();
619                         return false;
620                 }
621         }
622
623         /* Run tinc-up script to further initialize the tap interface */
624         xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
625         xasprintf(&envp[1], "DEVICE=%s", device ? : "");
626         xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
627         xasprintf(&envp[3], "NAME=%s", myself->name);
628         envp[4] = NULL;
629
630         execute_script("tinc-up", envp);
631
632         for(i = 0; i < 4; i++)
633                 free(envp[i]);
634
635         /* Run subnet-up scripts for our own subnets */
636
637         subnet_update(myself, NULL, true);
638
639         /* Open sockets */
640
641         listen_sockets = 0;
642         cfg = lookup_config(config_tree, "BindToAddress");
643
644         do {
645                 get_config_string(cfg, &address);
646                 if(cfg)
647                         cfg = lookup_config_next(config_tree, cfg);
648
649                 hint.ai_family = addressfamily;
650                 hint.ai_socktype = SOCK_STREAM;
651                 hint.ai_protocol = IPPROTO_TCP;
652                 hint.ai_flags = AI_PASSIVE;
653
654                 err = getaddrinfo(address, myport, &hint, &ai);
655                 free(address);
656
657                 if(err || !ai) {
658                         logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
659                                    gai_strerror(err));
660                         return false;
661                 }
662
663                 for(aip = ai; aip; aip = aip->ai_next) {
664                         if(listen_sockets >= MAXSOCKETS) {
665                                 logger(LOG_ERR, "Too many listening sockets");
666                                 return false;
667                         }
668
669                         listen_socket[listen_sockets].tcp =
670                                 setup_listen_socket((sockaddr_t *) aip->ai_addr);
671
672                         if(listen_socket[listen_sockets].tcp < 0)
673                                 continue;
674
675                         listen_socket[listen_sockets].udp =
676                                 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
677
678                         if(listen_socket[listen_sockets].udp < 0) {
679                                 close(listen_socket[listen_sockets].tcp);
680                                 continue;
681                         }
682
683                         event_set(&listen_socket[listen_sockets].ev_tcp,
684                                           listen_socket[listen_sockets].tcp,
685                                           EV_READ|EV_PERSIST,
686                                           handle_new_meta_connection, NULL);
687                         if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
688                                 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
689                                 abort();
690                         }
691
692                         event_set(&listen_socket[listen_sockets].ev_udp,
693                                           listen_socket[listen_sockets].udp,
694                                           EV_READ|EV_PERSIST,
695                                           handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
696                         if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
697                                 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
698                                 abort();
699                         }
700
701                         ifdebug(CONNECTIONS) {
702                                 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
703                                 logger(LOG_NOTICE, "Listening on %s", hostname);
704                                 free(hostname);
705                         }
706
707                         memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
708                         listen_sockets++;
709                 }
710
711                 freeaddrinfo(ai);
712         } while(cfg);
713
714         if(listen_sockets)
715                 logger(LOG_NOTICE, "Ready");
716         else {
717                 logger(LOG_ERR, "Unable to create any listening socket!");
718                 return false;
719         }
720
721         return true;
722 }
723
724 /*
725   initialize network
726 */
727 bool setup_network(void) {
728         init_connections();
729         init_subnets();
730         init_nodes();
731         init_edges();
732         init_requests();
733
734         if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
735                 if(pinginterval < 1) {
736                         pinginterval = 86400;
737                 }
738         } else
739                 pinginterval = 60;
740
741         if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
742                 pingtimeout = 5;
743         if(pingtimeout < 1 || pingtimeout > pinginterval)
744                 pingtimeout = pinginterval;
745
746         if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
747                 maxoutbufsize = 10 * MTU;
748
749         if(!setup_myself())
750                 return false;
751
752         return true;
753 }
754
755 /*
756   close all open network connections
757 */
758 void close_network_connections(void) {
759         splay_node_t *node, *next;
760         connection_t *c;
761         char *envp[5];
762         int i;
763
764         for(node = connection_tree->head; node; node = next) {
765                 next = node->next;
766                 c = node->data;
767                 c->outgoing = NULL;
768                 terminate_connection(c, false);
769         }
770
771         list_delete_list(outgoing_list);
772
773         if(myself && myself->connection) {
774                 subnet_update(myself, NULL, false);
775                 terminate_connection(myself->connection, false);
776                 free_connection(myself->connection);
777         }
778
779         for(i = 0; i < listen_sockets; i++) {
780                 event_del(&listen_socket[i].ev_tcp);
781                 event_del(&listen_socket[i].ev_udp);
782                 close(listen_socket[i].tcp);
783                 close(listen_socket[i].udp);
784         }
785
786         xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
787         xasprintf(&envp[1], "DEVICE=%s", device ? : "");
788         xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
789         xasprintf(&envp[3], "NAME=%s", myself->name);
790         envp[4] = NULL;
791
792         exit_requests();
793         exit_edges();
794         exit_subnets();
795         exit_nodes();
796         exit_connections();
797
798         execute_script("tinc-down", envp);
799
800         if(myport) free(myport);
801
802         for(i = 0; i < 4; i++)
803                 free(envp[i]);
804
805         devops.close();
806
807         return;
808 }