Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ap_sta_config.sh #3

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 71 additions & 125 deletions ap_sta_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ WIFI_MODE=${ARG_WIFI_MODE:-'g'}
COUNTRY_CODE=${ARG_COUNTRY_CODE:-'FR'}
AP_IP=${ARG_AP_IP:-'192.168.10.1'}
AP_IP_BEGIN=$(echo "${AP_IP}" | sed -e 's/\.[0-9]\{1,3\}$//g')
MAC_ADDRESS="$(cat /sys/class/net/wlan0/address)"

if ! test -v AP_ONLY; then
AP_ONLY="false"
Expand All @@ -189,178 +188,115 @@ if test true != "${STA_ONLY}" && test true == "${AP_ONLY}"; then

# keep order of dependencies installation
if [[ $(dpkg -l | grep -c cron) == 0 ]]; then
apt-get -y update
apt-get -y install cron
fi

if [[ $(dpkg -l | grep -c dhcpcd) == 0 ]]; then
apt-get -y update
apt-get -y install dhcpcd
fi

if [[ $(dpkg -l | grep -c hostapd) == 0 ]]; then
apt-get -y update
apt-get -y install hostapd
fi

if [[ $(dpkg -l | grep -c dnsmasq) == 0 ]]; then
apt-get -y update
apt-get -y install dnsmasq
apt -y update
apt -y install cron
apt -y install dhcpcd
apt -y install hostapd
apt -y install dnsmasq
fi
fi

if test true != "${STA_ONLY}"; then
# Populate `/etc/udev/rules.d/70-persistent-net.rules`
_logger "Populate /etc/udev/rules.d/70-persistent-net.rules"
bash -c 'cat > /etc/udev/rules.d/70-persistent-net.rules' <<EOF
SUBSYSTEM=="ieee80211", ACTION=="add|change", ATTR{macaddress}=="${MAC_ADDRESS}", KERNEL=="phy0", \
RUN+="/sbin/iw phy phy0 interface add ap0 type __ap", \
RUN+="/bin/ip link set ap0 address ${MAC_ADDRESS}

# Exclude ap0 from `/etc/dhcpcd.conf`
sudo bash -c 'cat >> /etc/dhcpcd.conf' << EOF
# this defines static addressing to ap@wlan0 and disables wpa_supplicant for this interface
interface ap@wlan0
static ip_address=${AP_IP}/24
ipv4only
nohook wpa_supplicant
EOF
fi

if test true != "${STA_ONLY}"; then
# Populate `/etc/dnsmasq.conf`
_logger "Populate /etc/dnsmasq.conf"
bash -c 'cat > /etc/dnsmasq.conf' <<EOF
interface=lo,ap0
bash -c 'cat > /etc/dnsmasq.conf' << EOF
interface=lo,ap0@wlan0
ipitio marked this conversation as resolved.
Show resolved Hide resolved
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
server=1.1.1.1
domain-needed
bogus-priv
dhcp-range=${AP_IP_BEGIN}.50,${AP_IP_BEGIN}.150,12h

dhcp-option=3,${AP_IP}
EOF
fi

if test true != "${STA_ONLY}"; then
# Populate `/etc/hostapd/hostapd.conf`
_logger "Populate /etc/hostapd/hostapd.conf"
bash -c 'cat > /etc/hostapd/hostapd.conf' <<EOF
bash -c 'test -f /etc/hostapd/hostapd.conf || cat > /etc/hostapd/hostapd.conf' << EOF
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
interface=ap0
interface=ap0@wlan0
ipitio marked this conversation as resolved.
Show resolved Hide resolved
driver=nl80211
ieee80211n=1
ssid=${AP_SSID}
hw_mode=${WIFI_MODE}
channel=11
wmm_enabled=0
wmm_enabled=1
macaddr_acl=0
auth_algs=1
wpa=2PASSPHRASE
wpa=2
$([ $AP_PASSPHRASE ] && echo "wpa_passphrase=${AP_PASSPHRASE}")
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
wpa_pairwise=TKIP
rsn_pairwise=CCMP

EOF
fi

sudo chmod 600 /etc/hostapd/hostapd.conf
sudo bash -c 'SYSTEMD_EDITOR=tee systemctl edit --force --full accesspoint@.service' << EOF
[Unit]
Description=IEEE 802.11 ap@%i AP on %i with hostapd
Wants=wpa_supplicant@%i.service
[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 %i interface add ap@%i type __ap
ExecStart=/usr/sbin/hostapd -i ap@%i -P /run/hostapd.pid -B $DAEMON_OPTS ${DAEMON_CONF}
ExecStopPost=-/sbin/iw dev ap@%i del
[Install]
WantedBy=sys-subsystem-net-devices-%i.device
EOF

# not used, as the agent is hooked by dhcpcd
sudo systemctl disable wpa_supplicant.service

if test true != "${STA_ONLY}"; then
# Populate `/etc/default/hostapd`
_logger "Populate /etc/default/hostapd"
bash -c 'cat > /etc/default/hostapd' <<EOF
DAEMON_CONF="/etc/hostapd/hostapd.conf"
# We can then follow Raspberry’s documentation to enable routing and IP masquerading:
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent

sudo bash -c '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
fi

if test true != "${AP_ONLY}"; then
# Populate `/etc/wpa_supplicant/wpa_supplicant.conf`
_logger "Populate /etc/wpa_supplicant/wpa_supplicant.conf"
bash -c 'cat > /etc/wpa_supplicant/wpa_supplicant.conf' <<EOF
sudo bash -c 'test -f /etc/wpa_supplicant/wpa_supplicant.conf || cat > /etc/wpa_supplicant/wpa_supplicant.conf' << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=${COUNTRY_CODE}
network={
ssid="${CLIENT_SSID}"
$([ $CLIENT_PASSPHRASE ] && echo "psk=\"${CLIENT_PASSPHRASE}\"")
id_str="AP1"
scan_ssid=1
}

EOF
fi

# Populate `/etc/network/interfaces`
# TODO manage eth0 interface
# if current device is model B+ with ethernet port
# auto eth0
# allow-hotplug eth0
# iface eth0 inet manual
#
_logger "Populate /etc/network/interfaces"
bash -c 'cat > /etc/network/interfaces' <<EOF
source-directory /etc/network/interfaces.d

auto lo
auto ap0
auto wlan0

iface lo inet loopback

allow-hotplug ap0
iface ap0 inet static
address ${AP_IP}
netmask 255.255.255.0
# network ${AP_IP_BEGIN}.0
# broadcast ${AP_IP_BEGIN}.255
# gateway ${AP_IP}
hostapd /etc/hostapd/hostapd.conf

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface AP1 inet dhcp

EOF

if test true != "${STA_ONLY}"; then
# Populate `/bin/manage-ap0-iface.sh`
_logger "Populate /bin/manage-ap0-iface.sh"
bash -c 'cat > /bin/manage-ap0-iface.sh' <<EOF
#!/bin/bash
# check if hostapd service succes to start or not
# in our case, it cannot start when /var/run/hostapd/ap0 exist
# so we have to delete it
echo 'Check if hostapd.service is hang cause ap0 exist...'
hostapd_is_running=\$(service hostapd status | grep -c "Active: active (running)")
if test 1 -ne "\${hostapd_is_running}"; then
rm -rf /var/run/hostapd/ap0 | echo "ap0 interface does not exist, the faillure is elsewhere"
fi

EOF
chmod +x /bin/manage-ap0-iface.sh
sudo chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
fi

if test true != "${STA_ONLY}"; then
# Populate `/bin/rpi-wifi.sh`
_logger "Populate /bin/rpi-wifi.sh"
bash -c 'cat > /bin/rpi-wifi.sh' <<EOF
#!/bin/bash
echo 'Starting Wifi AP and STA client...'
ifdown --force wlan0
ifdown --force ap0
ifup ap0
ifup wlan0
$([ "${NO_INTERNET-}" != "true" ] && echo "sysctl -w net.ipv4.ip_forward=1")
$([ "${NO_INTERNET-}" != "true" ] && echo "iptables -t nat -A POSTROUTING -s ${AP_IP_BEGIN}.0/24 ! -d ${AP_IP_BEGIN}.0/24 -j MASQUERADE")
$([ "${NO_INTERNET-}" != "true" ] && echo "systemctl restart dnsmasq")
echo 'WPA Supplicant reconfigure in 5sec...'
sleep 5
wpa_cli -i wlan0 reconfigure

EOF
chmod +x /bin/rpi-wifi.sh
fi

if test true != "${STA_ONLY}"; then
# unmask and enable dnsmasq.service / hostapd.service
_logger "Unmask and enable dnsmasq.service / hostapd.service"
systemctl unmask dnsmasq.service hostapd.service
systemctl enable dnsmasq.service hostapd.service
# enable dnsmasq.service / disable hostapd.service
_logger "enable dnsmasq.service / disable hostapd.service"
systemctl unmask dnsmasq.service
systemctl enable dnsmasq.service
sudo systemctl stop hostapd # if the default hostapd service was active before
sudo systemctl disable hostapd # if the default hostapd service was enabled before
sudo systemctl enable accesspoint@wlan0.service
sudo rfkill unblock wlan
systemctl daemon-reload
fi

Expand All @@ -369,6 +305,16 @@ mkdir -p /var/log/ap_sta_wifi
touch /var/log/ap_sta_wifi/ap0_mgnt.log
touch /var/log/ap_sta_wifi/on_boot.log

# Add firewall rules
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o ap@wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i ap@wlan0 -o wlan0 -j ACCEPT
sudo netfilter-persistent save

# persist powermanagement off for wlan0
grep 'iw dev wlan0 set power_save off' /etc/rc.local || sudo sed -i 's:^exit 0:iw dev wlan0 set power_save off\n\nexit 0:' /etc/rc.local

# Finish
if test true == "${STA_ONLY}"; then
wpa_cli -i wlan0 reconfigure
Expand All @@ -388,5 +334,5 @@ fi
if test true != "${STA_ONLY}"; then
_logger "Wait during wlan0 reconnecting to internet..."
sleep 15
curl https://raw.githubusercontent.com/MkLHX/AP_STA_RPI_SAME_WIFI_CHIP/master/ap_sta_cron.sh | bash -s --
#curl https://raw.githubusercontent.com/MkLHX/AP_STA_RPI_SAME_WIFI_CHIP/master/ap_sta_cron.sh | bash -s --
fi