3 # Device configuration script for tinc
4 # $Id: tinc-up,v 1.1.2.1 2000/11/24 16:52:57 zarq Exp $
6 # Based on Lubomir Bulej's Redhat init script.
8 # This file is called after the tap device is opened by tinc. The
9 # environment variable IFNAME contains the name of the device; NETNAME
10 # contains the name of the network that was started.
12 my $IFNAME=$ENV{"IFNAME"};
13 my $NETNAME=$ENV{"NETNAME"};
16 ##############################################################################
17 # vpn_load () Loads VPN configuration
19 # $_[0] ... VPN to load
23 $CFG="$TCONF/$_[0]/tinc.conf";
24 if(! open($CFG, "< $CFG")) {
25 warn "tinc: $CFG does not exist\n";
31 if( /^[ ]*TapDevice[ =]+([^ \#]+)/i ) {
34 $DEV =~ s/^.*\/([^\/0-9]+)([0-9]+)$/$1$2/;
36 } elsif ( /^[ ]*(MyOwnVPNIP|MyVirtualIP)[ =]+([^ \#]+)/i ) {
39 } elsif ( /^[ ]*VpnMask[ =]+([^ \#]+)/i ) {
45 warn "tinc: There must be a TapDevice\n";
49 warn "tinc: TapDevice should be of the form /dev/tapN\n";
53 warn "tinc: MyVirtualIP required\n";
57 warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
60 if(defined($VPNMASK) && $VPNMASK eq "") {
61 warn "tinc: Invalid argument to VpnMask\n";
66 $ADR =~ s/^([^\/]+)\/.*$/$1/;
68 $LEN =~ s/^.*\/([^\/]+)$/$1/;
69 if($ADR eq "" || $LEN eq "") {
70 warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
73 @addr = split(/\./, $ADR);
75 $ADR = pack('C4', @addr);
76 $MSK = pack('N4', -1 << (32 - $LEN));
77 $BRD = join(".", unpack('C4', $ADR | ~$MSK));
78 $MAC = "fe:fd:" . join(":", map { sprintf "%02x", $_ } unpack('C4', $ADR));
80 if(!defined($VPNMASK)) {
82 $VPNMASK = join(".", unpack('C4', $VPNMASK));
84 $ADR = join(".", unpack('C4', $ADR));
85 $MSK = join(".", unpack('C4', $MSK));
91 ##############################################################################
92 # vpn_start () starts specified VPN
94 # $_[0] ... VPN to start
97 vpn_load($_[0]) || return 0;
99 system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
100 system("ifconfig $DEV hw ether $MAC");
101 system("ifconfig $DEV $ADR netmask $VPNMASK broadcast $BRD mtu 1448 -arp");
102 system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
108 ##############################################################################
109 # vpn_stop () Stops specified VPN
111 # $_[0] ... VPN to stop
114 vpn_load($_[0]) || return 1;
116 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
118 system("ifconfig $DEV down");
119 system("rmmod ethertap$NUM -s");
123 if(!defined($ARGV[0])) {
124 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
127 if($ARGV[0] eq "start") {
129 print "Starting $DESC:";
135 } elsif ($ARGV[0] eq "stop") {
137 print "Stopping $DESC:";
143 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
145 print "Stopping $DESC:";
151 print "Starting $DESC:";
158 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";