OpenWRT: Rewriting Destination IP Address for Local Server Network LAN
In this article, we will discuss how to rewrite the destination IP address for a local server network LAN using OpenWRT 23.05. This is particularly useful when you have a local server that is accessible via specific IP addresses and ports, and you want to make it accessible via a different IP address within your LAN.
Prerequisites
Before we begin, make sure you have the following:
- A device running OpenWRT 23.05
- A local server with a static IP address
- A network with multiple devices
Configuring the Firewall
The first step is to configure the firewall to allow traffic to the new IP address. To do this, follow these steps:
- Log in to your OpenWRT device.
- Navigate to
Network > Firewall. - Click on the
Zonestab. - Create a new zone by clicking the
Addbutton. - Name the zone (e.g.,
lan_newip), and set the network to the new IP address of your server. - Click
Save & Apply. - Now, navigate to the
Firewall Rulestab. - Add a new rule by clicking the
Addbutton. - Set the protocol to
TCPorUDP, depending on your needs. - Set the source zone to
lanand the destination zone to the new zone you created. - Set the destination port to the port your server is listening on.
- Click
Save & Apply.
Rewriting the Destination IP Address
Now that the firewall is configured, we can move on to rewriting the destination IP address. To do this, we will use the iptables command.
First, we need to find the MAC address of the device with the new IP address. To do this, run the following command:
$ /sbin/ip neigh show
Find the MAC address for the new IP address, and make a note of it.
Next, we will add a new rule to iptables to rewrite the destination IP address. Run the following command:
$ /usr/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.150.42.7
Replace 80 with the port your server is listening on, and replace 10.150.42.7 with the IP address of your server. This command will rewrite the destination IP address for all incoming TCP traffic on port 80 to the IP address of your server.
To make this change persistent, add the following line to the /etc/firewall.user file:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.150.42.7
In this article, we have discussed how to rewrite the destination IP address for a local server network LAN using OpenWRT 23.05. We have covered the following steps:
- Configuring the firewall
- Rewriting the destination IP address using
iptables - Making the change persistent