Debian init.d script automatically sets tap device's MTU to 1448 now.
[oweals/tinc.git] / debian / init.d
1 #! /usr/bin/perl -w
2 #
3 # System startup script for tinc
4 # $Id: init.d,v 1.14 2000/06/01 20:21:27 guus Exp $
5 #
6 # Based on Lubomir Bulej's Redhat init script.
7 #
8 # Create a file $NETSFILE (/etc/tinc/nets.boot), and put all the names of
9 # the networks in there.  These names must be valid directory names under
10 # $TCONF (/etc/tinc).  Lines starting with a # will be ignored in this
11 # file.
12 #
13
14 my $DAEMON="/usr/sbin/tincd";
15 my $NAME="tinc";
16 my $DESC="tinc daemons";
17 my $TCONF="/etc/tinc";
18 my $EXTRA="";
19 my $NETSFILE="$TCONF/nets.boot";
20 my @NETS=();
21
22
23 if (! -f $DAEMON) { exit 0; }
24
25
26
27 sub find_nets {
28     if(! open(FH, $NETSFILE)) {
29         warn "Please create $NETSFILE.\n";
30         exit 0;
31     }
32     while (<FH>) {
33         chomp;
34         if( /^[ ]*([^ \#]+)/i ) {
35             push(@NETS, "$1");
36         }
37     }
38     if($#NETS == -1) {
39         warn "$NETSFILE doesn't contain any nets.\n";
40         exit 0;
41     }
42     
43 }
44
45
46 ##############################################################################
47 # vpn_load ()           Loads VPN configuration
48
49 # $_[0] ... VPN to load
50
51 sub vpn_load {
52     my @addr;
53     $CFG="$TCONF/$_[0]/tinc.conf";
54     if(! open($CFG, "< $CFG")) {
55         warn "tinc: $CFG does not exist\n";
56         return 0;
57     }
58
59     # load TINCD config
60     while(<$CFG>) {
61         if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
62             $DEV=$1;
63             chomp($DEV);
64             $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
65             $NUM = $2;
66         } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
67             $VPN=$2;
68             chomp($VPN);
69         } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
70             $VPNMASK=$1;
71         }
72     }
73     if(!defined($DEV)) {
74         warn "tinc: There must be a TapDevice\n";
75         return 0;
76     }
77     if($DEV eq "") {
78         warn "tinc: TapDevice should be of the form /dev/tapN\n";
79         return 0;
80     }
81     if(!defined($VPN)) {
82         warn "tinc: MyVirtualIP required\n";
83         return 0;
84     }
85     if($VPN eq "") {
86         warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
87         return 0;
88     }
89     if(defined($VPNMASK) && $VPNMASK eq "") {
90         warn "tinc: Invalid argument to VpnMask\n";
91         return 0;
92     }
93
94     $ADR = $VPN;
95     $ADR =~ s/^([^\/]+)\/.*$/$1/;
96     $LEN = $VPN;
97     $LEN =~ s/^.*\/([^\/]+)$/$1/;
98     if($ADR eq "" || $LEN eq "") {
99         warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
100         return 0;
101     }
102     @addr = split(/\./, $ADR);
103
104     $ADR = pack('C4', @addr);
105     $MSK = pack('N4', -1 << (32 - $LEN));
106     $BRD = join(".", unpack('C4', $ADR | ~$MSK));
107     $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
108
109     if(!defined($VPNMASK)) {
110         $VPNMASK = $MSK;
111     }
112     
113     $VPNMASK = pack('C4', split(/\./, $VPNMASK));
114     $VPNMASK = join(".", unpack('C4', $VPNMASK));
115     $ADR = join(".", unpack('C4', $ADR));
116     $MSK = join(".", unpack('C4', $MSK));
117     
118     1;
119 }
120
121
122 ##############################################################################
123 # vpn_start ()          starts specified VPN
124
125 # $_[0] ... VPN to start
126
127 sub vpn_start {
128     vpn_load($_[0]) || return 0;
129
130     system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
131     system("ifconfig $DEV hw ether $MAC");
132     system("ifconfig $DEV $ADR netmask $VPNMASK broadcast $BRD mtu 1448 -arp");
133     system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
134 }
135
136
137
138
139 ##############################################################################
140 # vpn_stop ()           Stops specified VPN
141 #
142 # $_[0] ... VPN to stop
143
144 sub vpn_stop {
145     vpn_load($_[0]) || return 1;
146
147     system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
148     
149     system("ifconfig $DEV down");
150     system("rmmod ethertap$NUM -s");
151 }
152
153
154 if(!defined($ARGV[0])) {
155     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
156 }
157
158 if($ARGV[0] eq "start") {
159     find_nets;
160     print "Starting $DESC:";
161     foreach $n (@NETS) {
162         print " $n";
163         vpn_start($n);
164     }
165     print ".\n";
166 } elsif ($ARGV[0] eq "stop") {
167     find_nets;
168     print "Stopping $DESC:";
169     foreach $n (@NETS) {
170         print " $n";
171         vpn_stop($n);
172     }
173     print ".\n";
174 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
175     find_nets;
176     print "Stopping $DESC:";
177     foreach $n (@NETS) {
178         print " $n";
179         vpn_stop($n);
180     }
181     print ".\n";
182     print "Starting $DESC:";
183     foreach $n (@NETS) {
184         print " $n";
185         vpn_start($n);
186     }
187     print ".\n";
188 } else {
189     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
190 }