#!/usr/bin/perl # # Original shell script to convert a KA9Q NOS format gateways route file # (usually called 'encap.txt') into a Linux routing table format for the # IP tunnel driver. G0LGS' perl version (2007) # # Change path above to your location of Perl # Usage: ./ipip.munge < encap.txt > ipip.routes # # Outside of this script you'll need to implement policy routing for # your local networks. I.e: #/sbin/ip rule add from 44.8.0.160 table default #/sbin/ip rule add from 44.0.0.160 to 44.0.0.8/ table main # # Ampr Gateway IPIP Tunnel Munge Script V1.1 for ipiproute2 # By Stewart Wilkinson (G0LGS) # Checks the IP Subnet / Netmask are Valid # Modified by Pontus Falk (sm0rux) $Vers="V2.0-1 2007 Oct 02"; #$DEBUG=1; # Usage: Gateway (encap) file on stdin, Linux route format file on stdout. # ie: ipip_munge < encap.txt > linux-routes # NOTE: The following should be checked/changed before using this script: # # 1) ${AMPR} Set to your Local Ampr IP (for Tunnel device) # # 2) ${INTADDR} Set to your Internet IP Address # As defined in the Ampr Gateways Files. # # 3) ${LOCAL} Your script to handle adding local tunnels (if any). # # 4) ${TUNL} Set to correct device for your system. # # 5) ${IP_OP} Additional Options for 'ip route add' # ${RO_OP} or older 'route add' commands. # (ie mss / window settings) # Your AMPR IP for the Tunnel Device ${AMPR}="44.8.0.160"; # Your Internet IP ${INTADDR}="70.137.68.217"; # Script to handle adding local tunnels (if any). ${LOCAL}="ipip_local"; # Tunnel Device ${TUNL}="tunl0"; # Extra Options for 'ip route add' ${IP_OP}=""; # Extra Options for old 'route add' ${RO_OP}="mss 512 window 1024"; #--------------------------------------------------------- # Nothing below should need Changing - Unless it's broken #--------------------------------------------------------- # Check if we have 'iproute' or if we need to use 'route' if( -x "/sbin/ip" ){ ${iproute}=1; ${options} = "onlink " . ${IP_OP}; }elsif( -x "/sbin/route" ){ ${iproute}=0; ${options} = ${RO_OP}; }else{ print STDERR "ERROR:\tUnable to find 'iproute' package or 'route' command\n"; print STDERR "\tCannot Continue - script aborted\n"; exit 1; } printf "#\n# IPIP tunnel Routes - built by %s on %s", $ENV{LOGNAME}, `date`; printf "# Created by ipip_munge %s (by G0LGS)\n#\n", $Vers; printf "#\necho Stopping ${TUNL}\n"; printf "/sbin/ifconfig ${TUNL} down\n#\n"; printf "/bin/sleep 2\n#\n"; printf "#\necho -n 'Starting ${TUNL} '\n"; printf "/sbin/ifconfig ${TUNL} ${AMPR} txqueuelen 1000 up\n"; printf "/sbin/ip route del 44.0.0.0/8\n"; printf "#\n"; $lines=0; while( $src = ){ chomp($src); # Ignore any non-route lines if( $src !~ /^route addprivate/ ){ next; } # Split line ($f1,$f2,$f3,$f4,$f5) = split( / /, $src, 5 ); # Ignore our own entries if( ${f5} =~ /^${INTADDR}$/ ){ next; } # IP Subnet / Netmask ($s1, $s2) = split( '\/', $f3 ); ($n1, $n2, $n3, $n4) = split( '\.', $s1 ); # Work around lines with Subnets:44..... if( $n1 =~ /subnets\:(\d*)/i ){ $n1 = $1; } if (! defined($n1) || $n1 =~ /^$/ ){ $n1 ="0"; } if (! defined($n2) || $n2 =~ /^$/){ $n2 ="0"; } if (! defined($n3) || $n3 =~ /^$/){ $n3 ="0"; } if (! defined($n4) || $n4 =~ /^$/){ $n4 ="0"; } if (! defined($s2) || $s2 =~ /^$/){ $s2 ="32"; } if( &CheckNetMask() == 1 ){ # Good IP / NM if( ${iproute} ){ if( $s2 == "32" ){ printf "echo %s\n", ${f5} if $DEBUG; printf "/sbin/ip route add %s.%s.%s.%s via %s dev %s %s\n", $n1, $n2, $n3, $n4, $f5, ${TUNL}, ${options}; }else{ printf "echo %s\n", ${f5} if $DEBUG; printf "/sbin/ip route add %s.%s.%s.%s/%s via %s dev %s %s\n", $n1, $n2, $n3, $n4, $s2, $f5, ${TUNL}, ${options}; } }else{ if( $s2 == "32" ){ printf "echo %s\n", ${f5} if $DEBUG; printf "/sbin/route add -host %s.%s.%s.%s gw %s dev %s %s\n", $n1, $n2, $n3, $n4, $f5, ${TUNL}, ${options}; }else{ printf "echo %s\n", ${f5} if $DEBUG; printf "/sbin/route add -net %s.%s.%s.%s/%s gw %s dev %s %s\n", $n1, $n2, $n3, $n4, $s2, $f5, ${TUNL}, ${options}; } } }else{ # Broken IP / NM printf "# Invalid Netmask: "; if( ${iproute} ){ if( $s2 == "32" ){ printf "/sbin/ip route add %s.%s.%s.%s via %s dev %s %s\n", $n1, $n2, $n3, $n4, $f5, ${TUNL}, ${options}; }else{ printf "/sbin/ip route add %s.%s.%s.%s/%s via %s dev %s %s\n", $n1, $n2, $n3, $n4, $s2, $f5, ${TUNL}, ${options}; } }else{ if( $s2 == "32" ){ printf "/sbin/route add -host %s.%s.%s.%s gw %s dev %s %s\n", $n1, $n2, $n3, $n4, $f5, ${TUNL}, ${options}; }else{ printf "/sbin/route add -net %s.%s.%s.%s/%s gw %s dev %s %s\n", $n1, $n2, $n3, $n4, $s2, $f5, ${TUNL}, ${options}; } } } $lines++; if( ! ($lines % 25) ){ printf "echo -n '.'\n"; } } # Add Local Routes if( -f ${LOCAL} ){ if( open(LOCAL, "< ${LOCAL}" ) ) { while( $L = ){ printf $L; } close(LOCAL); } }else{ printf "#\n#No Local Routes Found\n"; } printf "#\n# default the rest of amprnet via mirrorshades.ucsd.edu\n#\n"; if( ${iproute} ) { printf "/sbin/ip route add 44.0.0.0/8 via 169.228.66.251 dev %s %s\n", ${TUNL}, ${options}; }else{ printf "/sbin/route add -net 44.0.0.0/8 gw 169.228.66.251 dev %s %s\n", ${TUNL}, ${options}; } printf "\necho -e '\\nDone'\n# The End\n#\n"; exit; sub CheckNetMask { my($ret) = 0; # Calculate Netmask $NM = 0xFFFFFFFF; if( $s2 < 32 ){ $NM = $NM << (32 - $s2); } # Convert Address to Full 32 Bit Value $AD =( ( ( ( ($n1 << 8) + $n2) << 8) + $n3) << 8) +$n4; # Check the Mask if( ($AD & $NM) == $AD ){ $ret = 1; } return $ret; }