3 # System startup script for tinc
4 # $Id: init.d,v 1.6 2000/05/16 14:34:44 zarq Exp $
6 # Based on Lubomir Bulej's Redhat init script.
9 my $DAEMON="/usr/sbin/tincd";
11 my $DESC="tinc daemons";
12 my $TCONF="/etc/tinc";
15 # Put your VPN names in here.
16 my $NETS=""; # This is a space-separated list of networks to be started.
19 if (! -f $DAEMON) { exit 0; }
22 warn "Please edit /etc/init.d/tinc before attempting to start tinc.\n";
26 ##############################################################################
27 # vpn_load () Loads VPN configuration
29 # $_[0] ... VPN to load
34 $CFG="$TCONF/$_[0]/tinc.conf";
35 open($CFG, "< $CFG") || die "tinc: $CFG does not exist";
39 if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
42 $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
44 } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
47 } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
52 die "tinc: There must be a TapDevice";
55 die "tinc: TapDevice should be of the form /dev/tapN";
58 die "tinc: MyVirtualIP required";
61 die "tinc: No argument to MyVirtualIP/MyOwnVPNIP";
63 if(defined($VPNMASK) && $VPNMASK eq "") {
64 die "tinc: Invalid argument to VpnMask";
67 $ADR =~ s/^([^\/]+)\/.*$/$1/;
69 $LEN =~ s/^.*\/([^\/]+)$/$1/;
70 if($ADR eq "" || $LEN eq "") {
71 die "tinc: Badly formed MyVirtualIP/MyOwnVPNIP";
73 @addr = split(/\./, $ADR);
75 $ADR = pack('C4', @addr);
76 $MSK = pack('N4', -1 << (32 - $LEN));
77 $BRD = join(".", unpack('C4', $ADR | ~$MSK));
78 # $NET = join(".", unpack('C4', $ADR & $MSK));
79 $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
80 $ADR = join(".", unpack('C4', $ADR));
81 $MSK = join(".", unpack('C4', $MSK));
83 # print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
89 ##############################################################################
90 # vpn_start () starts specified VPN
92 # $_[0] ... VPN to start
95 vpn_load($_[0]) || die "tinc: could not vpn_load $_[0]";
97 system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
98 system("ifconfig $DEV hw ether $MAC");
99 system("ifconfig $DEV $ADR netmask $MSK broadcast $BRD -arp");
100 system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
101 if(defined($VPNMASK)) {
102 system("route add -net $ADR netmask $VPNMASK dev $DEV");
109 ##############################################################################
110 # vpn_stop () Stops specified VPN
112 # $_[0] ... VPN to stop
115 vpn_load($_[0]) || return 1;
117 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
119 system("ifconfig $DEV down");
120 system("rmmod ethertap$NUM -s");
124 if(!defined($ARGV[0])) {
125 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
128 if($ARGV[0] eq "start") {
129 print "Starting $DESC:";
130 foreach $n (split(" ", $NETS)) {
135 } elsif ($ARGV[0] eq "stop") {
136 print "Stopping $DESC:";
137 foreach $n (split(" ", $NETS)) {
142 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
143 print "Stopping $DESC:";
144 foreach $n (split(" ", $NETS)) {
149 print "Starting $DESC:";
150 foreach $n (split(" ", $NETS)) {
156 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";