SYNOPSYS

eth0 is connected to a dhcp filtered LAN.
I want to use an open Wifi network to access the internet without impacting the LAN connection.

PROCEDURE

/etc/conf.d/open_wifi_wpa.conf

1
2
3
4
5
6
# generated with: wpa_passphrase openwrtSSID openwrt_secret
network={
    ssid="openwrtSSID"
    #psk="openwrt_secret"
    psk=85fb1efxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx02225dc
}

/etc/conf.d/open_wifi.conf

1
2
3
4
5
6
wpa_config=/etc/conf.d/open_wifi_wpa.conf
ethx=eth0
interface=wlan0
gateway=180.30.20.10
network=180.0.0.0
netmask=255.0.0.0

/usr/lib/systemd/system/open-wifi.service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[Unit]
Description=OpenWrt Wifi
After=network.target
Wants=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/open_wifi.conf
ExecStart=/usr/bin/ip route del default via ${gateway}
ExecStart=/usr/bin/ip route add ${network}/8 dev ${ethx}
ExecStart=/usr/bin/wpa_supplicant -Dwext -i ${interface} -c ${wpa_config} -B
ExecStart=/usr/bin/dhcpcd -q -C resolv.conf -C mtu ${interface}
ExecStop=/usr/bin/ip link set ${interface} down
ExecStop=/usr/bin/ip route add default via ${gateway}
ExecStop=/usr/bin/ip ro
ExecStop=/usr/bin/ip addr flush dev ${interface

[Install]
WantedBy=multi-user.target

original route -n

1
2
3
4
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         180.30.20.10    0.0.0.0         UG    0      0        0 eth0
180.30.20.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0

route -n after systemctl start open-wifi.service

1
2
3
4
5
6
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.2.1     0.0.0.0         UG    303    0        0 wlan0
180.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 eth0
180.30.20.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan0