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