Search code examples
c++clinuxubuntuiptables

sharing internet connection from two different connections with iptables


I have wrote a c++ code to share internet from wlan0 or eth1 with eth0 and code works properly.

code to share eth1 with eth0:

cmd = "systemctl stop networking";
system(cmd.c_str());
cmd = "iptables -A FORWARD -o eth1 -i eth0 -s 192.168.2.0/24 -m conntrack --ctstate NEW -j ACCEPT";
system(cmd.c_str());
cmd = "ptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT";
system(cmd.c_str());
cmd = "iptables -t nat -F POSTROUTING";
system(cmd.c_str());
cmd = "iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE";
system(cmd.c_str());
cmd = "iptables-save | tee /etc/iptables.sav";
system(cmd.c_str());
cmd = "iptables-restore < /etc/iptables.sav";
system(cmd.c_str());
cmd = "sysctl net.ipv4.ip_forward=1";
system(cmd.c_str());
cmd = "ip route add default via 192.168.2.230";
system(cmd.c_str());
cmd = "/etc/init.d/networking restart";
system(cmd.c_str());

code to share wlan0 with eth0:

cmd = "systemctl stop networking";
system(cmd.c_str());
cmd = "iptables -A FORWARD -o wlan0 -i eth0 -s 192.168.2.0/24 -m conntrack --ctstate NEW -j ACCEPT";
system(cmd.c_str());
cmd = "ptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT";
system(cmd.c_str());
cmd = "iptables -t nat -F POSTROUTING";
system(cmd.c_str());
cmd = "iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE";
system(cmd.c_str());
cmd = "iptables-save | tee /etc/iptables.sav";
system(cmd.c_str());
cmd = "iptables-restore < /etc/iptables.sav";
system(cmd.c_str());
cmd = "sysctl net.ipv4.ip_forward=1";
system(cmd.c_str());
cmd = "ip route add default via 192.168.2.230";
system(cmd.c_str());
cmd = "/etc/init.d/networking restart";
system(cmd.c_str());

If I use one of those codes my connection will be shared with eth0 successfully, but now in some situations I need to switch between wlan0 and eth1 and if I try to use those codes again in my running application I got connection errors the errors I see:

Error: Connection activation failed: (5) IP configuration could not be reserved (no available address, timeout, etc.).

Error in connecting 113 - No route to host

it seems I can not just use the code twice in application while it's running, so what am I doing wrong and how should I do these configurations properly?


Solution

  • Well I have figured out the solution, first of all I have a SIMCARD and a WIFI connection, the SIMCARD connection was ppp0 instead of eth1, my first mistake...

    the second problem was when I had WIFI connected the ppp0 was not able to ping any destination and could not connect to internet, so I needed to turn off WIFI with "nmcli r wifi off" command, before sharing my ppp0 connection with eth0.

    so the final code is:

    code to share wlan0:

    iptables -A FORWARD -o wlan0 -i eth0 -s 192.168.2.0/24 -m conntrack --ctstate NEW -j ACCEPT
    iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    iptables -t nat -F POSTROUTING
    iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
    iptables-save | tee /etc/iptables.sav
    iptables-restore < /etc/iptables.sav
    sysctl net.ipv4.ip_forward=1
    ip route add default via 192.168.2.230
    /etc/init.d/networking restart
    

    code to clear wlan0 sharing and routing settings:

    iptables -D FORWARD -o wlan0 -i eth0 -s 192.168.2.0/24 -m conntrack --ctstate NEW -j ACCEPT
    iptables -D FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    iptables -t nat -F POSTROUTING
    iptables-save | tee /etc/iptables.sav
    iptables-restore < /etc/iptables.sav
    sysctl net.ipv4.ip_forward=0
    ip route del 192.168.2.0/24
    /etc/init.d/networking restart
    

    code to share ppp0:

    nmcli r wifi off 
    
    iptables -A FORWARD -o ppp0 -i eth0 -s 192.168.2.0/24 -m conntrack --ctstate NEW -j ACCEPT
    iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    iptables -t nat -F POSTROUTING
    iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
    iptables-save | tee /etc/iptables.sav
    iptables-restore < /etc/iptables.sav
    sysctl net.ipv4.ip_forward=1
    ip route add default via 192.168.2.230
    /etc/init.d/networking restart
    

    code to clear ppp0 sharing and routing settings:

    iptables -D FORWARD -o ppp0 -i eth0 -s 192.168.2.0/24 -m conntrack --ctstate NEW -j ACCEPT
    iptables -D FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    iptables -t nat -F POSTROUTING
    iptables-save | tee /etc/iptables.sav
    iptables-restore < /etc/iptables.sav
    sysctl net.ipv4.ip_forward=0
    ip route del 192.168.2.0/24
    /etc/init.d/networking restart