Use iptables and arptables to set a single ip address's primary and secondary routes
Source: Internet
Author: User
Sometimes, you only have one IP address, but there are multiple devices, such as the computer assigned by the company, your laptop, mobile phone, iPad, etc, obviously, the computer assigned by the company will be the top priority of your IP address, and other devices will not be able to access it. If you have a LinuxBOX with a Wi-Fi module, you may want to establish a secondary route. However, sometimes you only have one IP address, but there are multiple devices, such as computers distributed by the company, your laptop, mobile phone, iPad, etc. Obviously, the computer assigned by the company will be the top priority of your IP address, and other devices will not be connected. If you have a LinuxBOX with a Wi-Fi module, you may want to establish a secondary route. However, at this time, the machines distributed by your company will be hidden behind the BOX, if your BOX is converted to a status-based MASQURADE address on the WAN port, it is impossible for all active accesses to your hidden work machine, unless you configure complex DNAT rules or routing rules.
Now, things are different. you can configure this unique IP address on your work machine and use it on all other devices.
Step: assume that your BOX has three network ports: eth0, eth1, and wlan0. The following are a series of operations:
Your unique IP address and its configuration:
Address: 192.168.10.79/24
Gateway: 192.168.10.1
1. Bridge all these ports
Brctl addbf br0
Brctl addif br0 eth0
Brctl addif br0 eth1
Brctl addif br0 wlan0
2. configure an IP address for br0 for WIFI (the dhcp pool and DNS configuration process are omitted)
The maximum number of connected devices is as follows:
Ifconfig br0 172.20.1.1/28
3. Option 1: add a Secondary IP address to br0 to add the default route
Ifconfig br0: 0 192.168.10.255/23
Note that the above prefix23 is worth noting that the original subnet mask is 255.255.255.0, and the prefix is now reduced by 1 bit. now you can use the broadcast address in the original network as an IP address, this address does not conflict with any address.
Route add default gw 192.168.10.1
4. Option 2: Use the force-onlink route to add a route directly.
Ip route add 0.0.0.0/0 via 192.168.10.1 dev br0onlink
5. added the address conversion rule. all source addresses in the WIFI package are converted to your unique IP address.
Iptables-T mangle-a prerouting-m physdev -- physdev-inwlan0-j MARK -- set-mark 100
Iptables-t nat-a postrouting-m mark -- mark 100-j SNAT -- to-source 192.168.10.79
6. add arp if option 2 is selected.
Because the force-onlink route is used, it means that there is no IP address in the same network segment as 192.168.10.0/24 on your BOX. when you send a packet to the default gateway 192.168.10.1, ARP requests may not be replied, which involves the arp_filter of the default gateway or the configuration of the equivalent configuration, because the source of the arp request is the unique address 172.20.1.1, the requested address is not in the same CIDR block!
Therefore, an arptables rule is required:
Arptables-t mangle-a output-d 192.168.10.1-j mangle -- mangle-ip-s 192.168.10.79
In fact, when Option 1 is selected, you have configured a 192.168.10.255 address. for the original CIDR block, it is a broadcast, but for the BOX, it is a conventional IP address, therefore, ARP requests can be sent. the encapsulated source is 192.168.10.255 and can be returned normally. because the source in the request is 192.168.10.255, after it reaches the default gateway 192.168.10.1, it returns 192.168.10.0/24 with the mask 255.255.0, apparently belongs to the same network segment. check passed. REPLY returns!
Effect: the entire BOX is a HUB for your work machine, and its work is not affected! In addition, WIFI on the BOX leads to a subnet, which allows multiple devices to access. when the packets sent by these devices go out, the BOX will convert their source IP address to your unique IP address, the ip_conntrack mechanism ensures that the quintuple of all these device-related streams will not be repeated.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service