Code Monkey home page Code Monkey logo

4gvpn's Introduction

4GVPN

My setup for remote 4G VPN on Raspberry Pi using Sim7600e.

Setup

ECM mode

This is based on the following article: https://www.jeffgeerling.com/blog/2022/using-4g-lte-wireless-modems-on-raspberry-pi (read 2023-07-19)

For easiest configuration of the SIM7600x board, set to operate in ECM mode instead of QMI. In ECM mode, it will be detected as an USB modem (interface usb0) instead of wwan0 and you dont have to reset the board each time it boots.

  1. To set the boad in ECM mode, connect to the SIM7600x via serial interface with your favorite serial interface software (I use screen). Most probably it will be exposed on /dev/ttyUSB2.
screen /dev/ttyUSB2 115200
  1. Test connection by sending AT, it should respond with OK.
AT
OK
  1. Next, test if it's in USB mode with AT+QCFG="usbnet".
AT+QCFG="usbnet"
+QCFG: "usbnet",0

It might respond with ERROR, if this is the case go to step x.

  1. Set it in ECM mode with AT+QCFG="usbnet",1 and the modem will reboot, if not you can force a reboot with AT+CFUN=1,1.
  2. Exit screen session with ctrl-a ctrl-d.
  3. After a reboot you should now see usb0 listed as an interface from the comman ifconfig.
  4. If the output in step 3 was ERROR, try instead with sending AT+CUSBPIDSWITCH=9011,1,1. The modem should now restart, jump to step 5. (source https://www.waveshare.com/wiki/Raspberry_Pi_RNDIS_dial-up_Internet_access 2023-07-19)
  5. When the modem is in ECM mode, you can't access it via /dev/ttyUSB2, instead /dev/ttyS0 will be available.

Forward usb0 to eth0

This is based on the following article: https://newjerseystyle.github.io/en/2020/Raspberry-Pi-as-4G-LTE-Router/ (read 2023-07-19)

  1. Update your system:
$ sudo apt update
$ sudo apt upgrade
  1. Install dnsmasq:
$ sudo apt install dnsmasq
  1. Configure dnsmasq Create a file eth0 in the dnsmasq config directory.
$ sudo vim /etc/dnsmasq.d/eth0

With the following content:

interface=eth0                 # Use interface eth0  
listen-address=192.168.2.1     # listen on  
server=1.1.1.1                 # Forward DNS requests to Cloudflare DNS 
domain-needed                  # Don't forward short names  
bogus-priv                     # Never forward addresses in the non-routed address spaces.
# Assign IP addresses between 192.168.2.2 and 192.168.2.100 with a
# 12 hour lease time
dhcp-range=192.168.2.2,192.168.2.100,12h
  1. Configure interfaces Create a file 090-eht0 in the interfaces config directory.
$ sudo vim /etc/network/interfaces.d/090-eth0

With the following content:

allow-hotplug eth0  
iface eth0 inet static  
    address 192.168.2.1
    netmask 255.255.255.0
    network 192.168.2.0
    broadcast 192.168.2.255
  1. Configure forwarding by editing /etc/sysctl.conf, find the line #net.ipv4.ip_forwarding=1 and uncomment the line by removing # in the beginning.
  2. Reboot system
$ sudo reboot
  1. Configure NAT
$ sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE  
$ sudo iptables -A FORWARD -i usb0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT  
$ sudo iptables -A FORWARD -i eth0 -o usb0 -j ACCEPT
  1. Test if the configuration works before we make the NAT persistent. Connect a system via ethernet and ping 8.8.8.8 and google.com on the connected system to test if forwarding and DNS works, if it does then continue. If it doesn't work, you most probably have another default route enabled (like wlan0). This was my case so I had to disable the onboard WiFi, go to step 10.
$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=2 ttl=51 time=99.2 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=51 time=22.9 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=51 time=69.7 ms
  1. If the configuration works, the we can save the NAT config.
$ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Edit /etc/rc.local and add the following just above exit 0

iptables-restore < /etc/iptables.ipv4.nat
  1. Disable onboard WiFi.
sudo vim /boot/config.txt

Scroll to the bottom and you should find a section [all]. Under this section, add the folloing.

dtoverlay=disable-wifi

Forward usb0 to wlan0

Use RaspAP, https://github.com/RaspAP/raspap-webgui But from my testing is that it's quite slow, even on an RPi4. Maybe it performs better with a dedicated wifi-dongle.

Links

These are the sources i've been researching in order to setup everything.

Cloudflared DNS over HTTPS and Tunnel

SIM7600e

wwan0/usb0 to eth0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.