[be2a37e] | 1 | #!/bin/sh |
---|
| 2 | |
---|
[4a36a1b] | 3 | # set individualized routing table and policy for each network device |
---|
| 4 | |
---|
| 5 | |
---|
[be2a37e] | 6 | set -x |
---|
| 7 | |
---|
[8c804d8] | 8 | ## INTERFACES(5) |
---|
| 9 | # IFACE physical name of the interface being processed |
---|
[b9ae53a] | 10 | # METHOD method of the interface (e.g., static) |
---|
| 11 | # MODE start if run from ifup, stop if run from ifdown |
---|
| 12 | # PHASE as per MODE, but with finer granularity, distinguishing the pre- |
---|
| 13 | # up, post-up, pre-down and post-down phases. |
---|
[be2a37e] | 14 | |
---|
[8c804d8] | 15 | # optional arugument to set default gateway manually |
---|
| 16 | GATEWAY=$1 |
---|
[be2a37e] | 17 | |
---|
[84d8293] | 18 | TID=`/sbin/ip addr show dev ${IFACE} | grep -w ${IFACE}: | tr -s ' ' | cut -d : -f 1` |
---|
[8c804d8] | 19 | |
---|
| 20 | if [ "${PHASE}" = "post-down" ]; then |
---|
[be2a37e] | 21 | # delete routing table and policy and exit |
---|
| 22 | ## routing table is automatically flushed, so 'route flush' can be removed |
---|
| 23 | /sbin/ip route flush table ${TID} |
---|
| 24 | ## device has no longer any IP addr, so do not use IPADDR |
---|
| 25 | /sbin/ip rule del table ${TID} priority ${TID} |
---|
| 26 | /sbin/ip route flush cache |
---|
[8c804d8] | 27 | echo "remove routing table and policy for ${IFACE}" |
---|
[be2a37e] | 28 | exit |
---|
| 29 | fi |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | # get IP address, network mask, default gateway info |
---|
[84d8293] | 33 | IPADDR=`/sbin/ip addr show dev ${IFACE} | grep -w inet | tr -s ' ' | cut -d ' ' -f 3 | cut -d / -f 1` |
---|
[4a36a1b] | 34 | if [ -z "${IPADDR}" ]; then |
---|
[84c2881] | 35 | # fail to bind IP address to device |
---|
| 36 | echo "${IFACE} has no IP address." |
---|
| 37 | exit |
---|
| 38 | fi |
---|
[84d8293] | 39 | NETWORK=`/sbin/ip route show dev ${IFACE} | grep -w ${IPADDR} | tr -s ' ' | cut -d ' ' -f 1` |
---|
[be2a37e] | 40 | |
---|
| 41 | if [ -z "${GATEWAY}" ]; then |
---|
[84d8293] | 42 | GATEWAY=`/sbin/ip route show dev ${IFACE} | grep -w default | tr -s ' ' | cut -d ' ' -f 3` |
---|
[be2a37e] | 43 | fi |
---|
| 44 | if [ -z "${GATEWAY}" ]; then |
---|
[84d8293] | 45 | GATEWAY=`/sbin/ip route | grep -w default | tr -s ' ' | cut -d ' ' -f 3` |
---|
[be2a37e] | 46 | # this may add wrong default route especially on RHEL |
---|
| 47 | fi |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | # set routing table |
---|
[8c804d8] | 51 | /sbin/ip route add ${NETWORK} dev ${IFACE} table ${TID} |
---|
| 52 | /sbin/ip route add default via ${GATEWAY} dev ${IFACE} table ${TID} |
---|
[be2a37e] | 53 | |
---|
| 54 | # set routing policy |
---|
| 55 | /sbin/ip rule add from ${IPADDR} table ${TID} priority ${TID} |
---|
| 56 | |
---|
| 57 | /sbin/ip route flush cache |
---|
[8c804d8] | 58 | echo "add routing table and policy for ${IFACE} on table ${TID}" |
---|