Created
July 18, 2024 02:25
-
-
Save andikurnia/6c9ca8394bd2bc5166b3f212f3ae2480 to your computer and use it in GitHub Desktop.
Raspberry Pi Bridge Tethering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| [ $EUID -ne 0 ] && echo "run as root" >&2 && exit 1 | |
| ########################################################## | |
| # You should not need to update anything below this line # | |
| ########################################################## | |
| # parprouted - Proxy ARP IP bridging daemon | |
| # dhcp-helper - DHCP/BOOTP relay agent | |
| # dhcpcd - Roy Marples' DHCP client daemon | |
| # systemd-resolved - systemd network name resolution | |
| apt update && apt install -y parprouted dhcp-helper dhcpcd systemd-resolved | |
| systemctl stop dhcpcd dhcp-helper systemd-resolved | |
| systemctl enable dhcpcd dhcp-helper systemd-resolved | |
| # Enable the wpa_supplicant hook for dhcpcd. | |
| ln -s /usr/share/dhcpcd/hooks/10-wpa_supplicant /usr/lib/dhcpcd/dhcpcd-hooks/ | |
| # Enable ipv4 forwarding. | |
| sed -i'' s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/ /etc/sysctl.conf | |
| # Disable dhcpcd control of eth0. | |
| grep '^denyinterfaces eth0$' /etc/dhcpcd.conf || printf "denyinterfaces eth0\n" >> /etc/dhcpcd.conf | |
| # Configure dhcp-helper. | |
| cat > /etc/default/dhcp-helper <<EOF | |
| DHCPHELPER_OPTS="-b wlan0" | |
| EOF | |
| # Import the NetworkManager wifi connection profile. | |
| ssid="$(grep -r '^ssid=' /etc/NetworkManager/system-connections/preconfigured.nmconnection | awk '{print $2}' FS='[=]')" | |
| psk="$(grep -r '^psk=' /etc/NetworkManager/system-connections/preconfigured.nmconnection | awk '{print $2}' FS='[=]')" | |
| cat << EOF > "/etc/wpa_supplicant/wpa_supplicant.conf" | |
| ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev | |
| update_config=1 | |
| country=US | |
| network={ | |
| ssid="${ssid}" | |
| psk=${psk} | |
| key_mgmt=WPA-PSK | |
| } | |
| EOF | |
| # Enable avahi reflector if it's not already enabled. | |
| sed -i'' 's/#enable-reflector=no/enable-reflector=yes/' /etc/avahi/avahi-daemon.conf | |
| grep '^enable-reflector=yes$' /etc/avahi/avahi-daemon.conf || { | |
| printf "something went wrong...\n\n" | |
| printf "Manually set 'enable-reflector=yes in /etc/avahi/avahi-daemon.conf'\n" | |
| } | |
| # I have to admit, I do not understand ARP and IP forwarding enough to explain | |
| # exactly what is happening here. I am building off the work of others. In short | |
| # this is a service to forward traffic from WiFi to Ethernet. | |
| cat <<'EOF' >/usr/lib/systemd/system/parprouted.service | |
| [Unit] | |
| Description=proxy arp routing service | |
| Documentation=https://raspberrypi.stackexchange.com/q/88954/79866 | |
| Requires=sys-subsystem-net-devices-wlan0.device dhcpcd.service | |
| After=sys-subsystem-net-devices-wlan0.device dhcpcd.service | |
| [Service] | |
| Type=forking | |
| # Restart until wlan0 gained carrier | |
| Restart=on-failure | |
| RestartSec=5 | |
| TimeoutStartSec=30 | |
| # clone the dhcp-allocated IP to eth0 so dhcp-helper will relay for the correct subnet | |
| ExecStartPre=/bin/bash -c '/sbin/ip addr add $(/sbin/ip -4 -br addr show wlan0 | /bin/grep -Po "\\d+\\.\\d+\\.\\d+\\.\\d+")/32 dev eth0' | |
| ExecStartPre=/sbin/ip link set dev eth0 up | |
| ExecStartPre=/sbin/ip link set wlan0 promisc on | |
| ExecStart=-/usr/sbin/parprouted eth0 wlan0 | |
| ExecStopPost=/sbin/ip link set wlan0 promisc off | |
| ExecStopPost=/sbin/ip link set dev eth0 down | |
| ExecStopPost=/bin/bash -c '/sbin/ip addr del $(/sbin/ip -4 -br addr show wlan0 | /bin/grep -Po "\\d+\\.\\d+\\.\\d+\\.\\d+")/32 dev eth0' | |
| [Install] | |
| WantedBy=wpa_supplicant.service | |
| EOF | |
| systemctl disable NetworkManager | |
| systemctl daemon-reload | |
| systemctl enable parprouted |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked on Raspbian OS Bookworm | Raspberry 3/4