Use the new VpnMask directive to add a route to the rest of the VPN.
[tinc] / debian / init.d
1 #! /usr/bin/perl -w
2 #
3 # System startup script for tinc
4 # $Id: init.d,v 1.6 2000/05/16 14:34:44 zarq Exp $
5 #
6 # Based on Lubomir Bulej's Redhat init script.
7 #
8
9 my $DAEMON="/usr/sbin/tincd";
10 my $NAME="tinc";
11 my $DESC="tinc daemons";
12 my $TCONF="/etc/tinc";
13 my $EXTRA="";
14
15 # Put your VPN names in here.
16 my $NETS="";  # This is a space-separated list of networks to be started.
17
18
19 if (! -f $DAEMON) { exit 0; }
20
21 if ($NETS eq "") {
22     warn "Please edit /etc/init.d/tinc before attempting to start tinc.\n";
23     exit 0;
24 }
25
26 ##############################################################################
27 # vpn_load ()           Loads VPN configuration
28
29 # $_[0] ... VPN to load
30
31
32 sub vpn_load {
33     my @addr;
34     $CFG="$TCONF/$_[0]/tinc.conf";
35     open($CFG, "< $CFG") || die "tinc: $CFG does not exist";
36
37     # load TINCD config
38     while(<$CFG>) {
39         if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
40             $DEV=$1;
41             chomp($DEV);
42             $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
43             $NUM = $2;
44         } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
45             $VPN=$2;
46             chomp($VPN);
47         } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
48             $VPNMASK=$1;
49         }
50     }
51     if(!defined($DEV)) {
52         die "tinc: There must be a TapDevice";
53     }
54     if($DEV eq "") {
55         die "tinc: TapDevice should be of the form /dev/tapN";
56     }
57     if(!defined($VPN)) {
58         die "tinc: MyVirtualIP required";
59     }
60     if($VPN eq "") {
61         die "tinc: No argument to MyVirtualIP/MyOwnVPNIP";
62     }
63     if(defined($VPNMASK) && $VPNMASK eq "") {
64         die "tinc: Invalid argument to VpnMask";
65     }
66     $ADR = $VPN;
67     $ADR =~ s/^([^\/]+)\/.*$/$1/;
68     $LEN = $VPN;
69     $LEN =~ s/^.*\/([^\/]+)$/$1/;
70     if($ADR eq "" || $LEN eq "") {
71         die "tinc: Badly formed MyVirtualIP/MyOwnVPNIP";
72     }
73     @addr = split(/\./, $ADR);
74
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));
82     
83 #    print "$DEV $VPN $NUM $LEN @addr $MAC $MASK $BRD $NET\n";
84
85     1;
86 }
87
88
89 ##############################################################################
90 # vpn_start ()          starts specified VPN
91
92 # $_[0] ... VPN to start
93
94 sub vpn_start {
95     vpn_load($_[0]) || die "tinc: could not vpn_load $_[0]";
96
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");
103     }
104 }
105
106
107
108
109 ##############################################################################
110 # vpn_stop ()           Stops specified VPN
111 #
112 # $_[0] ... VPN to stop
113
114 sub vpn_stop {
115     vpn_load($_[0]) || return 1;
116
117     system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
118     
119     system("ifconfig $DEV down");
120     system("rmmod ethertap$NUM -s");
121 }
122
123
124 if(!defined($ARGV[0])) {
125     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
126 }
127
128 if($ARGV[0] eq "start") {
129     print "Starting $DESC:";
130     foreach $n (split(" ", $NETS)) {
131         print " $n";
132         vpn_start($n);
133     }
134     print ".\n";
135 } elsif ($ARGV[0] eq "stop") {
136     print "Stopping $DESC:";
137     foreach $n (split(" ", $NETS)) {
138         print " $n";
139         vpn_stop($n);
140     }
141     print ".\n";
142 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
143     print "Stopping $DESC:";
144     foreach $n (split(" ", $NETS)) {
145         print " $n";
146         vpn_stop($n);
147     }
148     print ".\n";
149     print "Starting $DESC:";
150     foreach $n (split(" ", $NETS)) {
151         print " $n";
152         vpn_start($n);
153     }
154     print ".\n";
155 } else {
156     die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
157 }