You can make the Raspberry Pi a router that routes ethernet packages between ethernet and wifi interface. It is not an access point. It connects by WiFi to the access point of the 4G modem. It is also not a bridge. Bridging on a wifi client connection is not supported by the on board wifi chip (1). From the view of the DSL router it may be a gateway to the internet, but I would say the real gateway is the 4G modem.
For a router you have to use different subnets, for example 192.168.4.0/24 for the 4G modem and the wifi connection to it and 192.168.10.0/24 for the wired ethernet connection to the DSL router. The most difficult thing on this setup could be that you have to configure the DSL router for its default gateway. There is no way around this.
I will show you how you can make the RasPi a router. I will use systemd-networkd for reasons. For reference I use Raspbian Stretch Lite 2018-06-27 updated with sudo apt update && sudo apt full-upgrade && sudo reboot
.
Example for this setup:
Client_A
4G NAT /
INTERNET <-> modem <~.wifi.~> (wlan0)RPi(eth0) <---wired---> router <--wan--> DSL
\ / \ / \
192.168.4.1 (dhcp) 192.168.10.60 192.168.10.1 Client_B
Setup systemd-networkd
For detailed information look at (2). Here only in short. Execute these commands:
rpi ~$ sudo -Es
rpi ~# systemctl mask networking.service
rpi ~# systemctl mask dhcpcd.service
rpi ~# sudo mv /etc/network/interfaces /etc/network/interfaces~
rpi ~# sed -i '1i resolvconf=NO' /etc/resolvconf.conf
rpi ~# systemctl enable systemd-networkd.service
rpi ~# systemctl enable systemd-resolved.service
rpi ~# ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
Setup wifi client connection
Setup wpa_supplicant with this file and your settings for country=
, ssid=
and psk=
and enable it. You can just copy and paste this in one block to your command line beginning with cat
and including EOF (delimiter EOF will not get part of the file):
rpi ~# cat >/etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="wlan@hoeft-online.de"
psk="verySecretPassword"
}
EOF
rpi # chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
rpi # systemctl disable wpa_supplicant.service
rpi # systemctl enable wpa_supplicant@wlan0.service
There is a service to start and stop wpa_supplicant for the wifi connection. We add to it rules to manage a network address translation (NAT) that we need to connect to the 4G modem without static routes. Create a drop in file (3):
rpi ~# systemctl edit wpa_supplicant@wlan0.service
In the empty editor insert these statements, save them and quit the editor. Pay attention to the minus sign =-
:
[Service]
ExecStartPost=/sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
ExecStopPost=-/sbin/iptables -t nat -D POSTROUTING -o wlan0 -j MASQUERADE
Configure interfaces
Create these files for interfaces eth0 and wlan0 with your settings. For eth0 don't use an ip address which is in the pool of the DHCP server from the DSL router.
rpi ~# cat > /etc/systemd/network/04-eth0.network <<EOF
[Match]
Name=eth0
[Network]
Address=192.168.10.60/24
IPForward=yes
EOF
rpi ~~# cat > /etc/systemd/network/08-wlan0.network <<EOF
[Match]
Name=wlan0
[Network]
DHCP=yes
EOF
Routing
To get routing complete working you have to configure the default gateway on the DSL router. You have to look how to configure it there. The DSL router is living on subnet 192.168.10.0/24 and it knows where to send packages with destination address of this subnet. But it does not know where to send packages with other destination addresses, means addresses to the internet. For this there is a default gateway to set. Set it on your DSL router to 192.168.10.60 so it sends all packages it does not know to this ip address. It's the RasPi and it knows where to go on. It sends the packages to its default gateway, that is 192.168.4.1, and so on. That's routing.
The RasPi has got its ip address on wlan0, e.g. 192.168.4.108, from the access point of the 4G modem. From that it has also got the default gateway to the 4G modem. It should look similar to this:
rpi ~$ ip route show
default via 192.168.4.1 dev wlan0 proto dhcp src 192.168.4.108 metric 1024
192.168.4.0/24 dev wlan0 proto kernel scope link src 192.168.4.108
192.168.4.1 dev eth0 proto dhcp scope link src 192.168.4.108 metric 1024
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.60
The important line is the first one with the default route. Don't be confused with this showing, it's only for understanding. It should be set automatically. You have to define the default route in the DSL router.
Reboot.
That's it.
refefences:
[1] Raspberry Pi WiFi to Ethernet Bridge for a server?
[2] Howto migrate from networking to systemd-networkd with dynamic failover
[3] man systemd.unit - overriding vendor settings