| 1 |
pcg |
1.1 |
#!/bin/bash |
| 2 |
|
|
|
| 3 |
|
|
# Some environment variables will be set: |
| 4 |
|
|
# |
| 5 |
|
|
# CONFBASE=/etc/vpe # the configuration directory prefix |
| 6 |
|
|
# IFNAME=vpn0 # the network interface (ifname) |
| 7 |
|
|
# MAC=fe:fd:80:00:00:01 # the mac-address to use for the interface |
| 8 |
|
|
# NODENAME=cerebro # the selected nodename (-n switch) |
| 9 |
|
|
# NODEID=1 # the numerical node id |
| 10 |
|
|
# MTU=1436 # the tunnel packet overhead (set mtu to 1500-$OVERHEAD) |
| 11 |
|
|
|
| 12 |
|
|
# this if-up script is rather full-featured, and is used to |
| 13 |
|
|
# generate a fully-routed (no arp traffic) vpn. the main portion |
| 14 |
|
|
# consists of "ipn" calls (see below). |
| 15 |
|
|
|
| 16 |
|
|
# some hosts require additional specific configuration, this is handled |
| 17 |
|
|
# using if statements near the end of the script. |
| 18 |
|
|
|
| 19 |
|
|
# with the --fw switch, outputs mac/net pairs for your firewall use: |
| 20 |
|
|
# if-up --fw | while read mac net; do |
| 21 |
|
|
# iptables -t filter -A INPUT -i vpn0 -p all -m mac --mac-source \! $mac -s $net -j DROP |
| 22 |
|
|
# done |
| 23 |
|
|
|
| 24 |
|
|
ipn() { |
| 25 |
|
|
local id="$1"; shift |
| 26 |
|
|
local mac=fe:fd:80:00:00:$(printf "%02x" $id) |
| 27 |
|
|
if [ -n "$FW" ]; then |
| 28 |
|
|
for net in "$@"; do |
| 29 |
|
|
echo "$mac $net" |
| 30 |
|
|
done |
| 31 |
|
|
else |
| 32 |
|
|
local ip="$1"; shift |
| 33 |
|
|
if [ "$id" == $NODEID ]; then |
| 34 |
|
|
[ -n "$ADDR_ONLY" ] && ip addr add $ip broadcast 10.255.255.255 dev $IFNAME |
| 35 |
|
|
elif [ -z "$ADDR_ONLY" ]; then |
| 36 |
|
|
ip neighbour add $ip lladdr $mac nud permanent dev $IFNAME |
| 37 |
|
|
for route in "$@"; do |
| 38 |
|
|
ip route add $route via $ip dev vpn0 |
| 39 |
|
|
done |
| 40 |
|
|
fi |
| 41 |
|
|
fi |
| 42 |
|
|
} |
| 43 |
|
|
|
| 44 |
|
|
ipns() { |
| 45 |
|
|
# this contains the generic routing information for the vpn |
| 46 |
|
|
# each call to ipn has the following parameters: |
| 47 |
|
|
# ipn <node-id> <gateway-ip> [<route> ...] |
| 48 |
|
|
# the second line (ipn 2) means: |
| 49 |
|
|
# the second node (doom in the config file) has the ip address 10.0.0.5, |
| 50 |
|
|
# which is the gateway for the 10.0/28 network and three additional ip |
| 51 |
|
|
# addresses |
| 52 |
|
|
|
| 53 |
|
|
ipn 1 10.0.0.20 |
| 54 |
|
|
ipn 2 10.0.0.5 10.0.0.0/28 200.100.162.92 200.100.162.93 100.99.218.222 |
| 55 |
|
|
ipn 3 10.0.0.17 |
| 56 |
|
|
ipn 4 10.0.0.18 |
| 57 |
|
|
ipn 5 10.0.0.19 |
| 58 |
|
|
ipn 6 10.0.0.21 10.0.2.0/26 200.100.162.17 |
| 59 |
|
|
ipn 7 10.0.0.22 10.1.2.0/24 |
| 60 |
|
|
ipn 8 10.0.0.23 |
| 61 |
|
|
ipn 9 10.0.0.24 10.13.0.0/16 |
| 62 |
|
|
ipn 10 10.0.0.25 |
| 63 |
|
|
ipn 11 10.0.0.26 |
| 64 |
|
|
ipn 12 10.0.0.27 10.0.2.64/26 |
| 65 |
|
|
ipn 13 10.0.0.28 10.0.3.0/24 |
| 66 |
|
|
ipn 14 10.0.0.29 10.1.1.0/24 |
| 67 |
|
|
# mind the gateway ip gap |
| 68 |
|
|
ipn 15 10.9.0.30 10.0.4.0/24 |
| 69 |
|
|
ipn 16 10.9.0.31 |
| 70 |
|
|
} |
| 71 |
|
|
|
| 72 |
|
|
if [ "$1" == "--fw" ]; then |
| 73 |
|
|
FW=1 |
| 74 |
|
|
|
| 75 |
|
|
ipns |
| 76 |
|
|
else |
| 77 |
|
|
exec >/var/log/vpe.if-up 2>&1 |
| 78 |
|
|
set -x |
| 79 |
|
|
|
| 80 |
|
|
[ $NODENAME = "ruth" ] && ip link set $IFNAME down # hack |
| 81 |
|
|
|
| 82 |
|
|
# first set the link up and initialize the interface ip |
| 83 |
|
|
# address. |
| 84 |
|
|
ip link set $IFNAME address $MAC mtu $MTU up |
| 85 |
|
|
ADDR_ONLY=1 ipns # set addr only |
| 86 |
|
|
|
| 87 |
|
|
# now initialize the main vpn routes (10.0/8) |
| 88 |
|
|
# the second route is a hack to to reach some funnily-connected |
| 89 |
|
|
# machines. |
| 90 |
|
|
ip route add 10.0.0.0/8 dev $IFNAME |
| 91 |
|
|
ip route add 10.0.0.0/27 dev $IFNAME |
| 92 |
|
|
|
| 93 |
|
|
ipns # set the interface routes |
| 94 |
|
|
|
| 95 |
|
|
# now for something completely different, ehr, something not |
| 96 |
|
|
# easily doable with ipn, namely some extra specific highly complicated |
| 97 |
|
|
# and non-regular setups for some machines. |
| 98 |
|
|
if [ $NODENAME = doom ]; then |
| 99 |
|
|
ip addr add 200.100.162.92 dev $IFNAME |
| 100 |
|
|
ip route add 200.100.0.0/16 via 10.0.0.17 dev $IFNAME |
| 101 |
|
|
ip route flush table 101 |
| 102 |
|
|
ip route add table 101 default src 200.100.162.92 via 10.0.0.17 dev $IFNAME |
| 103 |
|
|
|
| 104 |
|
|
ip addr add 100.99.218.222 dev $IFNAME |
| 105 |
|
|
ip route add 100.99.218.192/27 via 10.0.0.19 dev $IFNAME |
| 106 |
|
|
ip route flush table 103 |
| 107 |
|
|
ip route add table 103 default src 100.99.218.222 via 10.0.0.19 |
| 108 |
|
|
|
| 109 |
|
|
elif [ $NODENAME = marco ]; then |
| 110 |
|
|
ip addr add 200.100.162.17 dev $IFNAME |
| 111 |
|
|
ip route add 200.100.162.79 dev ppp0 |
| 112 |
|
|
ip route add 200.100.162.95 dev ppp0 |
| 113 |
|
|
ip route add 200.100.76.0/23 dev ppp0 |
| 114 |
|
|
ip route add src 200.100.162.17 200.100.0.0/16 via 10.0.0.17 dev $IFNAME |
| 115 |
|
|
|
| 116 |
|
|
fi |
| 117 |
|
|
|
| 118 |
|
|
# and this is the second part of the 10.0/27 hack. don't ask. |
| 119 |
|
|
[ $NODENAME != fwkw ] && ip route add 10.0.0.0/24 via 10.0.0.29 dev $IFNAME |
| 120 |
|
|
fi |
| 121 |
|
|
|
| 122 |
|
|
|