aredn/files/usr/local/bin/wifi-setup

203 lines
6.2 KiB
Bash
Executable File

#!/bin/sh
<<'LICENSE'
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2018 Joe Ayers ae6xe@arrl.net
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version.
LICENSE
# This script reads the AREDN related wireless config options and generates
# a wireless UCI config file
# $1 is directory to create the wireless config
dropdir=${1-/etc/config}
# get aredn wifi related parameters
configfile=/etc/config.mesh/_setup
while read -r line; do
if [ -n "$line" -a -z "${line##wifi*}" ]; then
value="${line##*= }"
value="${value##*=}"
eval ${line%% =*}="\"$value"\"
fi
done < $configfile
# get mesh RF dev master setting in /etc/config/network
meshif="$(uci -q get network.@device[2].name)"
meshphy="phy${meshif#wlan}"
# Configure WAN interface, which may be wireless or Ethernet
if [ $wifi3_enable -eq 1 ]; then
# switch WAN network config from Ethernet to Wireless interface
for i in `cat /etc/aredn_include/wan.network.config | cut -f3 -d' ' | sed -e "s/'//g"`; do
uci -q del_list network.@device[1].ports=$i
done
uci -q delete network.@device[1].type
# adding the wireless interface done later-below
uci -q commit network
fi
rm -f "${dropdir}/wireless"
touch "${dropdir}/wireless"
ifacecount=$(ls -d /sys/class/ieee80211/* | wc -l)
ifacenum=0
# find each phy and configure in wireless config
for _dev in /sys/class/ieee80211/*; do
[ -e "$_dev" ] || continue
dev="${_dev##*/}"
radio="radio${dev#phy}"
wlan="wlan${dev#phy}"
if [ -x /usr/bin/readlink -a -h "/sys/class/ieee80211/${dev}" ]; then
devpath="$(readlink -n -f /sys/class/ieee80211/${dev})"
else
devpath=""
fi
if [ -n "${devpath}" ]; then
devpath="${devpath##/sys/devices/}"
devpath="${devpath%%/ieee802*}"
case "${devpath}" in
platform*/pci*) devpath="${devpath##platform/}";;
esac
dev_id="set wireless.${radio}.path=${devpath}"
else
dev_id="set wireless.${radio}.macaddr=$(cat /sys/class/ieee80211/${dev}/macaddress)"
fi
is_mesh_rf=0
hwmode="11g"
htmode="HT20"
disabled="0"
chanbw=""
country=""
channel=""
distance=""
iw phy "${dev}" info | grep -q '5180 MHz' && {
hwmode="11a"
}
if [ "${dev}" = "${meshphy}" ]; then
# mesh RF adhoc configuration
is_mesh_rf=1
channel="${wifi_channel}"
chanbw="${wifi_chanbw}"
country="HX"
distance="${wifi_distance}"
ssid="${wifi_ssid}-${chanbw}-v3"
mode="adhoc"
encryption="none"
key=""
network="wifi"
elif [ $wifi2_enable -eq 1 -a \( $ifacecount -eq 1 -o \( \( $ifacecount -gt 1 \) -a \( "$hwmode" = "$wifi2_hwmode" \) \) \) ]; then
# LAN AP interface
channel="${wifi2_channel}"
ssid=$(echo "${wifi2_ssid}" | sed -e 's/\(..\)/\\x\1/g')
ssid=$(echo -e "${ssid}")
ssid=${ssid//\"/\\\"}
mode="ap"
encryption="${wifi2_encryption}"
key=$(echo "${wifi2_key}" | sed -e 's/\(..\)/\\x\1/g')
key=$(echo -e "${key}")
key=${key//\"/\\\"}
network="lan"
elif [ $wifi3_enable -eq 1 -a \( $ifacecount -eq 1 -o \( \( $ifacecount -gt 1 \) -a \( "$hwmode" = "$wifi3_hwmode" \) \) \) ]; then
# WAN Client
disabled="0"
channel=""
ssid=$(echo "${wifi3_ssid}" | sed -e 's/\(..\)/\\x\1/g')
ssid=$(echo -e "${ssid}")
ssid=${ssid//\"/\\\"}
mode="sta"
if [ -z "$wifi3_key" ]; then
encryption="none"
key=""
else
encryption="psk2"
key=$(echo "${wifi3_key}" | sed -e 's/\(..\)/\\x\1/g')
key=$(echo -e "${key}")
key=${key//\"/\\\"}
fi
network="wan"
htmode=""
uci -c ${dropdir} -q batch > /dev/null <<-EOF
set network.@device[1].name=$wlan
set network.wan.device=$wlan
EOF
uci -c ${dropdir} -q commit network
else
# interface is disabled
disabled="1"
fi
uci -c ${dropdir} -q batch > /dev/null <<-EOF
set wireless.${radio}=wifi-device
set wireless.${radio}.type=mac80211
set wireless.${radio}.disabled=${disabled}
set wireless.${radio}.channel=${channel}
set wireless.${radio}.chanbw=${chanbw}
set wireless.${radio}.country=${country}
set wireless.${radio}.distance=${distance}
set wireless.${radio}.hwmode=${hwmode}
set wireless.${radio}.htmode=${htmode}
${dev_id}
add wireless wifi-iface
set wireless.@wifi-iface[${ifacenum}].ifname=${wlan}
set wireless.@wifi-iface[${ifacenum}].device=${radio}
set wireless.@wifi-iface[${ifacenum}].network=${network}
set wireless.@wifi-iface[${ifacenum}].mode=${mode}
set wireless.@wifi-iface[${ifacenum}].ssid="${ssid}"
set wireless.@wifi-iface[${ifacenum}].encryption=${encryption}
set wireless.@wifi-iface[${ifacenum}].key="${key}"
EOF
uci -c ${dropdir} -q commit wireless
if [ ${is_mesh_rf} -eq 1 ]; then
# add monitor interface on mesh rf
ifacenum=$((ifacenum + 1))
uci -c ${dropdir} -q batch > /dev/null <<-EOF
add wireless wifi-iface
set wireless.@wifi-iface[${ifacenum}].ifname=${wlan}-1
set wireless.@wifi-iface[${ifacenum}].device=${radio}
set wireless.@wifi-iface[${ifacenum}].network=wifi_mon
set wireless.@wifi-iface[${ifacenum}].mode=monitor
EOF
uci -q -c ${dropdir} commit wireless
fi
ifacenum=$((ifacenum + 1))
done