network nat Port forwarding - ghdrako/doc_snipets GitHub Wiki

NAT - technika przesyłania ruchu sieciowego poprzez router, która wiąże się ze zmianą źródłowych lub docelowych adresów IP1(https://pl.wikipedia.org/wiki/Network_Address_Translation#cite_note-1), zwykle również numerów portów TCP/UDP pakietów IP podczas ich przepływu. Zmieniane są także sumy kontrolne (zarówno w pakiecie IP, jak i w segmencie TCP/UDP), aby potwierdzić wprowadzone zmiany.

Większość systemów korzystających z NAT ma na celu umożliwienie dostępu wielu hostom w sieci prywatnej do Internetu przy wykorzystaniu pojedynczego publicznego adresu IP (zob. brama sieciowa).

Dwa podstawowe typy NAT:

  • SNAT (Source Network Address Translation) to technika polegająca na zmianie adresu źródłowego pakietu IP na jakiś inny. Stosowana często w przypadku podłączenia sieci dysponującej adresami prywatnymi do sieci Internet. Wtedy router, przez który podłączono sieć, podmienia adres źródłowy prywatny na adres publiczny (najczęściej swój własny).
  • DNAT (Destination Network Address Translation) to technika polegająca na zmianie adresu docelowego pakietu IP na jakiś inny. Stosowana często w przypadku, gdy serwer, który ma być dostępny z Internetu ma tylko adres prywatny. W tym przypadku router dokonuje translacji adresu docelowego pakietów IP z Internetu na adres tego serwera.

W przypadku systemu operacyjnego GNU/Linux funkcje NAT definiowane są za pomocą programów iptables lub ipchains.

To set up NAT using iptables, you can use the following basic commands:

 echo 1 > /proc/sys/net/ipv4/ip_forward

This will enable forwarding, which allows your machine to forward packets from one interface to another. This particular command updates the Linux kernel configuration on the fly by using the proc filesystem. You can achieve the same thing using the sysctl command:

sudo sysctl -w net.ipv4.ip_forward=1

To configure NAT for a local network such as 192.168.10.0/24, you will need to run the following commands as root user:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

Note that eth0 is the interface connected to the internet, and eth1 is the interface connected to the internal network. The MASQUERADE target is used to implement NAT on a Linux router. When packets pass through the router and are sent out to the internet, the MASQUERADE target changes the source address of the packets to the router’s public IP address. This allows devices on the internal network to communicate with devices on the internet using the router’s public IP address as a source address, effectively hiding the internal network from the internet. The MASQUERADE target is typically used in the POSTROUTING chain of the nat table and is commonly applied to the interface that is connected to the internet. It works only with dynamically assigned IP addresses (DHCP) and it’s mostly used in the case of a home router.

Port forwarding

Port forwarding is a technique that’s used to direct network traffic from one network address and port to another. This can be useful for directing incoming traffic to a specific service or application running on a computer or network device. This helps with accessing services or applications on a private network from a remote location, or for making services or applications running on a private network accessible to the public. Essentially, it’s another use of NAT as you will change the destination IP (and also port) of the packet that arrived on your machine. To forward packets coming into TCP port 80 on the eth0 interface to internal IP 192.168.10.101 on port 8080 using iptables, you can use the following commands:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.101:8080
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

We need MASQUERADE here as we want to hide the internal IP from the outside.