Thursday, October 27, 2011

Howto: ip iproute


一張網卡 bind 兩個不同網段的 IP

 

環境: 兩條對外網路, 兩個 ATU-R 均接到 Hub, 電腦接 Hub
IP1 100.100.100.100/24 Gateway 100.100.100.254
IP2 200.200.200.200/24 Gateway 200.200.200.254
ifconfig eth0 100.100.100.100 netmask 255.255.255.0
ifconfig eth0:0 200.200.200.200 netmask 255.255.255.0
由於 default gateway 只能設一個, 所以會有其中一個 IP 不通
必需用 ip route2 來解決這個問題
kernel option 中的 IP: policy routing 必需勾選(CONFIG_IP_MULTIPLE_TABLES)
否則在使用 ip rule 時會出現如下錯誤訊息
# ip rule list
RTNETLINK error: Invalid argument
dump terminated
先在 /etc/iproute2/rt_tables 下建兩個 table
echo "100 line1" >> /etc/iproute2/rt_tables 
echo "200 line2" >> /etc/iproute2/rt_tables
# 設定 line1 的 gateway
ip route add default via 100.100.100.254 table line1
# 設定 line2 的 gateway
ip route add default via 200.200.200.254 table line2
# 指定從 100.100.100.100 進來的連線走 rule line1
ip rule add from 100.100.100.100 table line1 
# 指定從 200.200.200.200 進來的連線走 rule line1
ip rule add from 200.200.200.200 table line2
設完後, 從外面連兩個 IP 都可以通
連外負載平衡
kernel option 中的 IP: equal cost multipath 必需勾選(CONFIG_IP_ROUTE_MULTIPATH)
指定 multipath
ip route add default scope global nexthop via 100.100.100.254 dev eth0 weight 1 \
nexthop via 200.200.200.254 dev eth0 weight 1
設完後就達成 line1 line2 路由平衡, 可以調整 weight 參數來決定 line1 或 line2 的比重
ip route list 會看到 default 如下
default 
nexthop via 100.100.100.254 dev eth0 weight 1
nexthop via 200.200.200.254 dev eth0 weight 1


Multi-PATH實作

 

用Linux來作路由器相當方便且效能又高, 可以自訂路由規則,彈性很高.
這次的目標主要是要整合 固定IP區段/ADSL單一IP虛擬, 作成多路由,讓使用者可以自由切換線路,又可保由固定IP的區段.
ADSL為單一IP偽裝區段(192.168.1.0/24),以eth1為出口,  固定IP區段為DMZ(非軍事區210.243.128.0/24), 以eth2為出口.
#!/bin/bash
# 設定 LAN
LAN="192.168.1.0" # subnet
LANM="24" # netmask
# 設定 Wan_1
WAN1="123.0.244.118" # 第一條 ADSL 的固定 IP
GW1="123.0.244.254" # 第一條 ADSL 的 Gateway
# 設定 Wan_2
WAN2="210.243.128.221" # 第二條 ADSL 的固定 IP
GW2="210.243.128.222" # 第二條 ADSL 的 Gateway
# pref 為 priority,值越小 priority 越高
# from 設定來源 IP
# table 設定 table 的編號
# 設定 Lan 的 ip rule  ,內部網路互做路由,不偽裝
ip rule add pref 95 from 192.168.1.0/24 to 192.168.1.0/24 table 95
ip rule add pref 96 from 210.243.128.0/24 to 210.243.128.0/24 table 96
#要去210.243.128.222的封包由table 97處理 , teble 97就指定由eth2出去
ip rule add pref 97 to 210.243.128.222 table 97
ip route replace 210.243.128.222 dev eth2 table 97
#要去210.243.128.0/24的封包由table 98處理 , teble 98就指定由eth0出去
ip rule add pref 98 to 210.243.128.0/24 table 98
ip route replace 210.243.128.0/24 dev eth0 table 98
#要去192.168.1.0/24的封包由table 99處理 , teble 99就指定由eth0出去
ip rule add pref 99 to $LAN/$LANM table 99 
ip route replace $LAN/$LANM dev eth0 table 99
# 設定第一條 ADSL 的 ip rule
ip rule add pref 100 from $WAN1 table 100 
ip route replace default via $GW1 table 100
# 設定第二條 ADSL 的 ip rule
ip rule add pref 101 from $WAN2 table 101 
ip route replace default via $GW2 table 101

# SeedNet ISP Routing (如果由210.243.128.0/24來的封包由table 103處理, teble 103就指定由eth2出去)
ip rule add pref 103 from 210.243.128.0/24 table 103
ip route replace default via $GW2 dev eth2 table 103
# Cable ISP Routing (如果由192.168.1.0/24來的封包由table 104處理, teble 104就指定由eth1出去)
ip rule add pref 104 from 192.168.1.0/24 table 104
ip route replace default via $GW1 dev eth1 table 104
# 清除 route cache
ip route flush cache
#最後將DMZ非軍事區設定ARP偽裝將其導向LINUX轉發路由
arp -i eth2 -s 210.243.128.1 00:01:02:03:04:05 pub
arp -i eth2 -s 210.243.128.2 00:01:02:03:04:05 pub
....... 以下類推
arp -i eth0 -s 210.243.128.222 00:11:22:33:44:55 pub

************2010/2/11 補充
如果ADSL斷線重連, 會發現 ROUTE 表被更動, 無法正常運作
ip route show table  xxx  可觀看此規則的 route gateway
重連後就是這行被清空, 要再重建一次, 否則會失效喔! 注意!


No comments:

Post a Comment