2 # ip source routing ifupdown script
3 # (C) 2015 Nirgal Vourgère
6 # This script set up source routing for a given interface
7 # Optionnal argument $1: The gateway. If missing, the interface gateway is required.
8 # Example usage: In /etc/network/interfaces
9 # iface eth7 inet static
10 # address 192.168.5.42/24
11 # post-up source-route 192.168.1.1
13 # iface eth7 inet static
14 # address 192.168.5.42/24
16 # post-up source-route
20 LOGGER="logger -t source-route"
22 if test "$VERBOSITY" = 1
24 echo " IFACE=$IFACE LOGICAL=$LOGICAL ADDRFAM=$ADDRFAM METHOD=$METHOD MODE=$MODE PHASE=$PHASE VERBOSITY=$VERBOSITY PATH=$PATH" >&2
25 $LOGGER -- IFACE=$IFACE LOGICAL=$LOGICAL ADDRFAM=$ADDRFAM METHOD=$METHOD MODE=$MODE PHASE=$PHASE VERBOSITY=$VERBOSITY PATH=$PATH
32 if ! which ipv4calc >/dev/null
34 critical_log "ipv4calc not found: apt-get install libnetwork-ipv4addr-perl"
37 if ! which ipv6calc >/dev/null
39 critical_log "ipv6calc not found: apt-get install ipv6calc"
44 critical_log "\$IFACE var is empty. Are you really running from /etc/network/interfaces ?"
47 if test "$PHASE" != "post-up"
49 critical_log "Invalid \$PHASE. $0 is only valid in 'post-up'."
53 RT_TABLES=/etc/iproute2/rt_tables
54 if ! grep -q $RTTABLE /etc/iproute2/rt_tables
56 critical_log "$RT_TABLES does not have $RTTABLE entry."
68 echo \$ADDRFAM must be inet or inet6. Ignoring. >&2
72 IPADDR=$(ip $IPFLAG -o addr show $IFACE | grep -w $ADDRFAM | grep global | sed -re "s/.*$ADDRFAM (.*)\/.*/\1/")
73 IPCIDR=$(ip $IPFLAG -o addr show $IFACE | grep -w $ADDRFAM | grep global | sed -re "s/.*$ADDRFAM .*\/([^ ]*).*/\1/")
74 if test "$ADDRFAM" = "inet6"
76 IPNET=$(ipv6calc --in ipv6 --out ipv6 --maskprefix $IPADDR/$IPCIDR)
78 IPNET=$(ipv4calc --network $IPADDR/$IPCIDR)/$IPCIDR
84 IPGW=$(ip $IPFLAG -o route show | grep default | sed -re "s/.*via ([^ ]*) .*/\1/")
88 critical_log "Could not find the gateway."
92 # Setting up source routing on $IFACE, so that packets from $IFACE don't get answered on another interface
93 echo "Setting up source routing on $IFACE: from $IPADDR -> route $IPNET via $IPGW" >&2
94 $LOGGER -- "Setting up source routing on $IFACE: from $IPADDR -> route $IPNET via $IPGW"
96 if ! ip $IPFLAG route show table $RTTABLE | grep -q default # Once only
98 if test "$VERBOSITY" = 1
100 $LOGGER -- ip $IPFLAG route add $IPNET dev "$IFACE" scope link table $RTTABLE
101 $LOGGER -- ip $IPFLAG route add default via "$IPGW" dev "$IFACE" table $RTTABLE
103 ip $IPFLAG route add $IPNET dev "$IFACE" scope link table $RTTABLE
104 ip $IPFLAG route add default via "$IPGW" dev "$IFACE" table $RTTABLE
106 if ! ip $IPFLAG rule show | grep -q $RTTABLE # Once only
108 if test "$VERBOSITY" = 1
110 $LOGGER -- ip $IPFLAG rule add from $IPADDR table $RTTABLE
112 ip $IPFLAG rule add from $IPADDR table $RTTABLE
115 ip $IPFLAG route flush cache