Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # pb-route, Copyright (C) 2010 Phillip Smith
- # This program comes with ABSOLUTELY NO WARRANTY; This is free software, and you are
- # welcome to use and redistribute it under the conditions of the GPL license version 2
- # See the "COPYING" file for further details.
- #
- # Setting up multiple routing tables
- /usr/sbin/ip route flush table 1 2>/dev/null
- /usr/sbin/ip rule del fwmark 101 table 1 2>/dev/null
- /usr/sbin/ip route add table 1 172.31.12.1 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 1 172.31.12.5 dev tun0 proto kernel scope link src 172.31.12.6 2>/dev/null
- /usr/sbin/ip route add table 1 59.167.195.32/29 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 1 192.168.31.0/27 dev eth0 proto kernel scope link src 192.168.31.20 metric 202 2>/dev/null
- /usr/sbin/ip route add table 1 192.168.0.0/23 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 1 172.31.0.0/16 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 1 default via 202.14.166.254 dev eth0
- /usr/sbin/ip rule add fwmark 101 table 1
- /usr/sbin/ip route flush table 2 2>/dev/null
- /usr/sbin/ip rule del fwmark 102 table 2 2>/dev/null
- /usr/sbin/ip route add table 2 172.31.12.1 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 2 172.31.12.5 dev tun0 proto kernel scope link src 172.31.12.6 2>/dev/null
- /usr/sbin/ip route add table 2 59.167.195.32/29 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 2 192.168.31.0/27 dev eth0 proto kernel scope link src 192.168.31.20 metric 202 2>/dev/null
- /usr/sbin/ip route add table 2 192.168.0.0/23 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 2 172.31.0.0/16 via 172.31.12.5 dev tun0 2>/dev/null
- /usr/sbin/ip route add table 2 default via 75.127.96.1 dev ppp200
- /usr/sbin/ip rule add fwmark 102 table 2
- # Flushing all chains in the "mangle" table
- /usr/sbin/iptables -t mangle -F
- /usr/sbin/iptables -t mangle -X
- # Setting up marking chains
- /usr/sbin/iptables -t mangle -N MARK-gw1
- /usr/sbin/iptables -t mangle -A MARK-gw1 -m comment --comment 'send via 202.14.166.254' -j MARK --set-mark 101
- /usr/sbin/iptables -t mangle -A MARK-gw1 -j CONNMARK --save-mark
- /usr/sbin/iptables -t mangle -A MARK-gw1 -j RETURN
- /usr/sbin/iptables -t mangle -N MARK-gw2
- /usr/sbin/iptables -t mangle -A MARK-gw2 -m comment --comment 'send via 75.127.96.1' -j MARK --set-mark 102
- /usr/sbin/iptables -t mangle -A MARK-gw2 -j CONNMARK --save-mark
- /usr/sbin/iptables -t mangle -A MARK-gw2 -j RETURN
- # Initializing "mangle" table
- # ==> Handle connection streams that have already been marked
- /usr/sbin/iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment "this stream is already marked; escape early" -m mark ! --mark 0 -j ACCEPT
- # ==> Handle incoming connection streams to route back via where they came in
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'prevent asynchronous routing' -i eth0 -m state --state NEW -j MARK-gw1
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'prevent asynchronous routing' -i ppp200 -m state --state NEW -j MARK-gw2
- # Setting up DESTINATION based routing policies (2 policies)
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment '74.125.0.0/16 via gw1' -m state --state NEW -d 74.125.0.0/16 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment '74.125.0.0/16 via gw1' -m state --state NEW -d 74.125.0.0/16 -j ACCEPT
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment '74.125.0.0/16 via gw1' -m state --state NEW -d 74.125.0.0/16 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment '74.125.0.0/16 via gw1' -m state --state NEW -d 74.125.0.0/16 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment '209.0.0.0/8 via gw1' -m state --state NEW -d 209.0.0.0/8 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment '209.0.0.0/8 via gw1' -m state --state NEW -d 209.0.0.0/8 -j ACCEPT
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment '209.0.0.0/8 via gw1' -m state --state NEW -d 209.0.0.0/8 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment '209.0.0.0/8 via gw1' -m state --state NEW -d 209.0.0.0/8 -j ACCEPT
- # Setting up PORT based routing policies (4 policies)
- /usr/sbin/iptables -t mangle -N PORT_80_gw2
- /usr/sbin/iptables -t mangle -A PORT_80_gw2 -m comment --comment 'tcp 80 via gw2' -p tcp --dport 80 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A PORT_80_gw2 -m comment --comment 'tcp 80 via gw2' -p tcp --dport 80 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_80_gw2 -m comment --comment 'udp 80 via gw2' -p udp --dport 80 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A PORT_80_gw2 -m comment --comment 'udp 80 via gw2' -p udp --dport 80 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_80_gw2 -j RETURN
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'tcp 80 via gw2' -m state --state NEW -j PORT_80_gw2
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment 'tcp 80 via gw2' -m state --state NEW -j PORT_80_gw2
- /usr/sbin/iptables -t mangle -N PORT_443_gw2
- /usr/sbin/iptables -t mangle -A PORT_443_gw2 -m comment --comment 'tcp 443 via gw2' -p tcp --dport 443 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A PORT_443_gw2 -m comment --comment 'tcp 443 via gw2' -p tcp --dport 443 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_443_gw2 -m comment --comment 'udp 443 via gw2' -p udp --dport 443 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A PORT_443_gw2 -m comment --comment 'udp 443 via gw2' -p udp --dport 443 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_443_gw2 -j RETURN
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'tcp 443 via gw2' -m state --state NEW -j PORT_443_gw2
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment 'tcp 443 via gw2' -m state --state NEW -j PORT_443_gw2
- /usr/sbin/iptables -t mangle -N PORT_21_gw1
- /usr/sbin/iptables -t mangle -A PORT_21_gw1 -m comment --comment 'tcp 21 via gw1' -p tcp --dport 21 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A PORT_21_gw1 -m comment --comment 'tcp 21 via gw1' -p tcp --dport 21 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_21_gw1 -m comment --comment 'udp 21 via gw1' -p udp --dport 21 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A PORT_21_gw1 -m comment --comment 'udp 21 via gw1' -p udp --dport 21 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_21_gw1 -j RETURN
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'tcp 21 via gw1' -m state --state NEW -j PORT_21_gw1
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment 'tcp 21 via gw1' -m state --state NEW -j PORT_21_gw1
- /usr/sbin/iptables -t mangle -N PORT_22_gw2
- /usr/sbin/iptables -t mangle -A PORT_22_gw2 -m comment --comment 'tcp 22 via gw2' -p tcp --dport 22 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A PORT_22_gw2 -m comment --comment 'tcp 22 via gw2' -p tcp --dport 22 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_22_gw2 -m comment --comment 'udp 22 via gw2' -p udp --dport 22 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A PORT_22_gw2 -m comment --comment 'udp 22 via gw2' -p udp --dport 22 -j ACCEPT
- /usr/sbin/iptables -t mangle -A PORT_22_gw2 -j RETURN
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'tcp 22 via gw2' -m state --state NEW -j PORT_22_gw2
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment 'tcp 22 via gw2' -m state --state NEW -j PORT_22_gw2
- # Setting up PROTOCOL based routing policies (1 policies)
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'icmp via gw2' -m state --state NEW -p icmp -j MARK-gw2
- /usr/sbin/iptables -t mangle -A PREROUTING -m comment --comment 'icmp via gw2' -m state --state NEW -p icmp -j ACCEPT
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment 'icmp via gw2' -m state --state NEW -p icmp -j MARK-gw2
- /usr/sbin/iptables -t mangle -A OUTPUT -m comment --comment 'icmp via gw2' -m state --state NEW -p icmp -j ACCEPT
- # Setting up DEFAULT routing policy
- /usr/sbin/iptables -t mangle -N DEF_POL
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'default balancing' -p tcp -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'default balancing' -p udp -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw1 tcp' -m mark --mark 0 -p tcp -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw1 tcp' -m mark --mark 0 -p tcp -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j ACCEPT
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw2 tcp' -m mark --mark 0 -p tcp -m state --state NEW -m statistic --mode nth --every 2 --packet 1 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw2 tcp' -m mark --mark 0 -p tcp -m state --state NEW -m statistic --mode nth --every 2 --packet 1 -j ACCEPT
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw1 udp' -m mark --mark 0 -p udp -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j MARK-gw1
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw1 udp' -m mark --mark 0 -p udp -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j ACCEPT
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw2 udp' -m mark --mark 0 -p udp -m state --state NEW -m statistic --mode nth --every 2 --packet 1 -j MARK-gw2
- /usr/sbin/iptables -t mangle -A DEF_POL -m comment --comment 'balance gw2 udp' -m mark --mark 0 -p udp -m state --state NEW -m statistic --mode nth --every 2 --packet 1 -j ACCEPT
- # Setting up Source NATs
- # ==> Adding SNAT for 192.168.0.0/16
- /usr/sbin/iptables -t nat -A POSTROUTING -m comment --comment 'snat outbound eth0' -o eth0 -s 192.168.0.0/16 -m mark --mark 101 -j SNAT --to-source 202.14.166.120
- /usr/sbin/iptables -t nat -A POSTROUTING -m comment --comment 'snat outbound ppp200' -o ppp200 -s 192.168.0.0/16 -m mark --mark 102 -j SNAT --to-source 75.127.96.189
- # ==> Adding SNAT for 172.16.0.0/12
- /usr/sbin/iptables -t nat -A POSTROUTING -m comment --comment 'snat outbound eth0' -o eth0 -s 172.16.0.0/12 -m mark --mark 101 -j SNAT --to-source 202.14.166.120
- /usr/sbin/iptables -t nat -A POSTROUTING -m comment --comment 'snat outbound ppp200' -o ppp200 -s 172.16.0.0/12 -m mark --mark 102 -j SNAT --to-source 75.127.96.189
- # ==> Adding SNAT for 10.0.0.0/8
- /usr/sbin/iptables -t nat -A POSTROUTING -m comment --comment 'snat outbound eth0' -o eth0 -s 10.0.0.0/8 -m mark --mark 101 -j SNAT --to-source 202.14.166.120
- /usr/sbin/iptables -t nat -A POSTROUTING -m comment --comment 'snat outbound ppp200' -o ppp200 -s 10.0.0.0/8 -m mark --mark 102 -j SNAT --to-source 75.127.96.189
- # Flushing route cache so new routes take effect
- /usr/sbin/ip route flush cache
Advertisement
Add Comment
Please, Sign In to add comment