3 # Device configuration script for tinc
4 # $Id: tinc-up,v 1.1.2.2 2000/12/22 16:54:56 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 ) {
48 warn "tinc: TapDevice should be of the form /dev/tapN\n";
52 warn "tinc: MyVirtualIP required\n";
56 warn "tinc: No argument to MyVirtualIP/MyOwnVPNIP\n";
59 if(defined($VPNMASK) && $VPNMASK eq "") {
60 warn "tinc: Invalid argument to VpnMask\n";
65 $ADR =~ s/^([^\/]+)\/.*$/$1/;
67 $LEN =~ s/^.*\/([^\/]+)$/$1/;
68 if($ADR eq "" || $LEN eq "") {
69 warn "tinc: Badly formed MyVirtualIP/MyOwnVPNIP\n";
72 @addr = split(/\./, $ADR);
74 $ADR = pack('C4', @addr);
75 $MSK = pack('N4', -1 << (32 - $LEN));
76 $BRD = join(".", unpack('C4', $ADR | ~$MSK));
77 $MAC = "fe:fd:00:00:00:00";
79 if(!defined($VPNMASK)) {
81 $VPNMASK = join(".", unpack('C4', $VPNMASK));
83 $ADR = join(".", unpack('C4', $ADR));
84 $MSK = join(".", unpack('C4', $MSK));
90 ##############################################################################
91 # vpn_start () starts specified VPN
93 # $_[0] ... VPN to start
96 vpn_load($_[0]) || return 0;
98 system("insmod ethertap -s --name=\"ethertap$NUM\" unit=\"$NUM\" >/dev/null");
99 system("ifconfig $DEV hw ether $MAC");
100 system("ifconfig $DEV $ADR netmask $VPNMASK broadcast $BRD mtu 1448 -arp");
101 system("start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA");
107 ##############################################################################
108 # vpn_stop () Stops specified VPN
110 # $_[0] ... VPN to stop
113 vpn_load($_[0]) || return 1;
115 system("start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$_[0].pid --exec $DAEMON -- -n $_[0] $EXTRA -k");
117 system("ifconfig $DEV down");
118 system("rmmod ethertap$NUM -s");
122 if(!defined($ARGV[0])) {
123 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";
126 if($ARGV[0] eq "start") {
128 print "Starting $DESC:";
134 } elsif ($ARGV[0] eq "stop") {
136 print "Stopping $DESC:";
142 } elsif ($ARGV[0] eq "restart" || $ARGV[0] eq "force-reload") {
144 print "Stopping $DESC:";
150 print "Starting $DESC:";
157 die "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}\n";