I set up the internet on a virtual machine that is running through VirtualBox and set up port forwarding. First, in rt_tables of Host Machine (iproutes2) I added this:
100 vm0
Then I run this:
ip rule add from 192.168.56.100 table vm0
ip route add default dev eth0 via 10.0.1.1 table vm0
192.168.56.100 is VM's local IP address. 10.0.1.1 is my gateway. And also 192.168.56.1 is Host's local IP address in "Host-Only" adapter.
And then iptables settings on Host
iptables -t filter -I FORWARD --in-interface vboxnet0 --out-interface eth0 --source 192.168.56.100 -j ACCEPT
iptables -t filter -I FORWARD --in-interface eth0 --out-interface vboxnet0 --destination 192.168.56.100 -j ACCEPT
iptables -t nat -A POSTROUTING -o vboxnet0 -j MASQUERADE
iptables -A FORWARD -i eth0 -j ACCEPT
iptables -A FORWARD -i vboxnet0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i vboxnet0 -j ACCEPT
iptables -A FORWARD -i eth0 -o vboxnet0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 15:1722 -j DNAT --to-destination 192.168.56.100
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1724:65530 -j DNAT --to-destination 192.168.56.100
iptables -t nat -A PREROUTING -i eth0 -p udp --dport 15:1722 -j DNAT --to-destination 192.168.56.100
iptables -t nat -A PREROUTING -i eth0 -p udp --dport 1724:65530 -j DNAT --to-destination 192.168.56.100
Sorry. Maybe some iptables rules are redundant here.
The VM has internet connection through "Host-Only" adapter and can accept all connections on forwarded ports. The problem is that Host doesn't say to VM client's real IP address. VM receives 192.168.56.1 instead of client's real IP. Example:
root@vm:~# lsof -i :22 -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 691 root 3u IPv4 21232 0t0 TCP *:22 (LISTEN)
sshd 691 root 4u IPv6 21234 0t0 TCP *:22 (LISTEN)
sshd 996826 root 4u IPv4 2857061 0t0 TCP 192.168.56.100:22->192.168.56.1:61441 (ESTABLISHED)