Skip to content

Instantly share code, notes, and snippets.

@idlecool
Created April 6, 2013 18:34
Show Gist options
  • Save idlecool/5327113 to your computer and use it in GitHub Desktop.
Save idlecool/5327113 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Start DHCPD Server
## Should be added to upstart, will do later
dhcpd
## <-- CONFIGURE NETWORK -->
LOCAL_ROUTER="172.16.0.1"
LOCAL_SUBNET="172.16.0.0/24"
LOCAL_INTERFACE="eth0"
UPLINK1_IP="172.16.1.254"
UPLINK1_GATEWAY="172.16.1.1"
UPLINK1_INTERFACE="eth1"
UPLINK1_TABLE="101"
UPLINK2_IP="172.16.2.254"
UPLINK2_GATEWAY="172.16.2.1"
UPLINK2_INTERFACE="eth2"
UPLINK2_TABLE="102"
#Setup forwarding and settings
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 10 > /proc/sys/net/ipv4/route/gc_timeout
#Combining Two Gateways
sudo ip route add default scope global nexthop via $UPLINK1_GATEWAY dev $UPLINK1_INTERFACE weight 1 nexthop via $UPLINK2_GATEWAY dev $UPLINK2_INTERFACE weight 1
# Add Local Network to Uplink Routng Table
ip route add $LOCAL_SUBNET dev $LOCAL_INTERFACE table $UPLINK1_TABLE
ip route add default via $UPLINK1_GATEWAY dev $UPLINK1_INTERFACE table $UPLINK1_TABLE
ip route add $LOCAL_SUBNET dev $LOCAL_INTERFACE table $UPLINK2_TABLE
ip route add default via $UPLINK2_GATEWAY dev $UPLINK2_INTERFACE table $UPLINK2_TABLE
# Load balencing Logic
ip rule add fwmark 1 table $UPLINK1_TABLE
ip rule add fwmark 2 table $UPLINK2_TABLE
iptables -t mangle -A POSTROUTING -o $UPLINK1_INTERFACE -j MARK --set-mark 1
iptables -t mangle -A POSTROUTING -o $UPLINK2_INTERFACE -j MARK --set-mark 2
iptables -t mangle -A POSTROUTING -j CONNMARK --save-mark
iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
###Local Network Rules
/sbin/iptables -A INPUT -i lo -j ACCEPT
/sbin/iptables -A OUTPUT -o lo -j ACCEPT
/sbin/iptables -A OUTPUT -o $LOCAL_INTERFACE -j ACCEPT
###Enable router to WAN communication -- This is a hack -- In my testing, the router itself could not talk to the net while hosts could
/sbin/iptables -A OUTPUT -o $UPLINK1_INTERFACE -j ACCEPT
/sbin/iptables -A OUTPUT -o $UPLINK2_INTERFACE -j ACCEPT
###NAT Routing
/sbin/iptables -t nat -A POSTROUTING -s $LOCAL_SUBNET -o $UPLINK1_INTERFACE -j MASQUERADE
/sbin/iptables -t nat -A POSTROUTING -s $LOCAL_SUBNET -o $UPLINK2_INTERFACE -j MASQUERADE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment