Skip to content

Instantly share code, notes, and snippets.

@adityastic
Last active August 20, 2023 14:37
Show Gist options
  • Save adityastic/61f9701deef830946e16daad9befd61b to your computer and use it in GitHub Desktop.
Save adityastic/61f9701deef830946e16daad9befd61b to your computer and use it in GitHub Desktop.
Raspberry PI 4B Wifi-Repeater with VPN

Raspberry Pi 4B Wifi-Extender/Repeater Setup with VPN Hotspot

(dhcp 192.168.1.3 from router)  +----------------------+   (192.168.1.1
                             \  |                      |  / +DHCP server)
                 wifi        (eth0)      wifi uplink   | /
mobile-phone <~.~.~.~.~> (ap0)RPi(wlan0) <.~.~.~.~.> router <───> INTERNET
            ╲             ╱          ╲                        wan
(dhcp 192.168.50.50 (192.168.50.1   (dhcp 192.168.1.2
          from RPi) +DHCP server)    from router)
  1. Shift to sudo:

    sudo -Es
  2. Install DNSMasq & Hostapd:

    apt install -y dnsmasq hostapd
  3. Configure HostAPd for Access Point:

    SSID="<Your SSID>"
    PASS="<Your Password>"
    
    test -f /etc/hostapd/hostapd.conf || cat > /etc/hostapd/hostapd.conf <<\EOF
    ctrl_interface=/var/run/hostapd
    driver=nl80211
    country_code=IE
    ssid=$SSID
    hw_mode=g
    channel=7
    auth_algs=1
    wpa=2
    wpa_passphrase=$PASS
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
    EOF
    
    chmod 600 /etc/hostapd/hostapd.conf
  4. Validate hardware:

    • Validate supported interface:

      iw list | grep "Supported interface modes" -A 8

      The following should list the interfaces:

              Supported interface modes:
               * IBSS
               * managed
               * AP
               * P2P-client
               * P2P-GO
               * P2P-device

      We are interested in AP. Which signifies we the driver supports Access Point as a supported interface.

    • Validate interface combinations:

      iw list | grep "Supported interface modes" -A 8

      The following should list the valid combinations:

              valid interface combinations:
                      * #{ managed } <= 1, #{ P2P-device } <= 1, #{ P2P-client, P2P-GO } <= 1,
                      total <= 3, #channels <= 2
                      * #{ managed } <= 1, #{ AP } <= 1, #{ P2P-client } <= 1, #{ P2P-device } <= 1,
                      total <= 4, #channels <= 1
              Device supports scan flush.
              Device supports randomizing MAC-addr in sched scans.
              Supported extended features:
                      * [ 4WAY_HANDSHAKE_STA_PSK ]: 4-way handshake with PSK in station mode

      Here, it means that not more than one AP or P2P-GO interface can be configured at the same time.

  5. Test access point creation:

    /sbin/iw dev wlan0 interface add uap0 type __ap
    /sbin/iw dev uap0 del

Note: The above command should not return any errors.

  1. Create SystemCtl service:

    SYSTEMD_EDITOR=tee systemctl edit --force --full uap@.service <<\EOF
    [Unit]
    Description=IEEE 802.11 %p%i AP on wlan%i with hostapd
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/run/hostapd.pid
    Restart=on-failure
    RestartSec=2
    Environment=DAEMON_CONF=/etc/hostapd/hostapd.conf
    EnvironmentFile=-/etc/default/hostapd
    ExecStartPre=/sbin/iw dev wlan%i interface add %p%i type __ap
    ExecStart=/usr/sbin/hostapd -i %p%i -P /run/hostapd.pid -B $DAEMON_OPTS ${DAEMON_CONF}
    ExecStopPost=-/sbin/iw dev %p0 del
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    systemctl stop hostapd # if the default hostapd service was active before
    systemctl disable hostapd # if the default hostapd service was enabled before
    systemctl enable uap@0.service
    rfkill unblock wlan

    In case you want to edit the file in the future:

    sudo -Es
    export SYSTEMD_EDITOR=vi systemctl edit --full uap@.service
    exit
  2. Now we setup wpa_supplicant for client connections.

    We need to update dhcpcd (the default a DHCP client) by editing /etc/dhcpcd.conf, adding the following to the end (these settings replace the configuration suggested in Raspberry’s site, which defined a static address to wlan0 instead of a DHCP Client; ref. "Define the wireless interface IP configuration"):

    # these two lines are not strictly needed, as wlan0 uses the default configuration
    interface wlan0 
        dhcp
    
    # this defines static addressing to uap0 and disables wpa_supplicant for this interface
    interface uap0
        static ip_address=192.168.50.1/24
        ipv4only
        nohook wpa_supplicant
    
  3. Install iptables, netfilter.

    sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent
  4. Create a file for routing ap:

    test -f /etc/sysctl.d/routed-ap.conf || cat >/etc/sysctl.d/routed-ap.conf <<\EOF
    # https://www.raspberrypi.org/documentation/configuration/wireless/access-point-routed.md
    # Enable IPv4 routing
    net.ipv4.ip_forward=1
    EOF
  5. Configure DNSMasq:

    cat >> /etc/dnsmasq.conf <<\EOF
    
    # disables dnsmasq reading any other files like /etc/resolv.conf for nameservers
    no-resolv
    
    interface=uap0
    no-dhcp-interface=lo,wlan0
    domain-needed
    bogus-priv
    server=8.8.8.8
    dhcp-range=192.168.50.50,192.168.50.199,12h
    dhcp-option=3,192.168.50.1
    EOF
  6. Install OpenVPN:

    apt install openvpn -y
  7. Download or sync .ovpn file from local terminal.

    rsync -chavzP --stats ~/Downloads/client.ovpn rupalsingh@192.168.0.179:~/OVPN/
  8. Copy ovpn file to /etc/openvpn/

    sudo mv ~/OVPN/client.ovpn /etc/openvpn/home.conf
  9. Validate OpenVPN works by running:

    sudo openvpn --config "/etc/openvpn/home.conf"
  10. Set OpenVPN to run autostart with home.conf. Edit /etc/default/openvpn by running:

    sudo nano /etc/default/openvpn

    And uncomment #AUTOSTART="all" and change all to home. The line should look like this:

    AUTOSTART="home"
  11. Add firewall rules:

    sudo iptables -F
    sudo iptables -t nat -F
    sudo iptables -X
    
    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
    sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
    
    sudo iptables -A FORWARD -i wlan0 -o uap0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i uap0 -o wlan0 -j ACCEPT
    sudo iptables -A FORWARD -i tun0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i wlan0 -o tun0 -j ACCEPT
    sudo netfilter-persistent save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment