diff --git a/lnxrouter b/lnxrouter index 4dc7af3..2fd90e6 100755 --- a/lnxrouter +++ b/lnxrouter @@ -1,7 +1,7 @@ #!/bin/bash -VERSION=0.6.7 -PROGNAME="$(basename $0)" +VERSION=0.7.3a +PROGNAME="$(basename "$0")" export LC_ALL=C @@ -88,8 +88,7 @@ Options: Using this you can't use same wlan interface for both Internet and AP --virt-name Set name of virtual interface - -c Channel number (default: 1) - --force_channel Force the use of channel provided by -c flag + -c Specify channel (default: use current, or 1 / 36) --country Set two-letter country code for regularity (example: US) --freq-band Set frequency band: 2.4 or 5 (default: 2.4) @@ -104,29 +103,42 @@ Options: --hostapd-debug 1 or 2. Passes -d or -dd to hostapd --isolate-clients Disable wifi communication between clients --no-haveged Do not run haveged automatically when needed - --hs20 Enable Hotspot 2.0 (Make sure your hostapd build supports it) + --hs20 Enable Hotspot 2.0 - WiFi 4(802.11N) Config: - --ieee80211n Enable IEEE 802.11n (HT) - --use_ht Enable High Throughput mode - --ht_capab HT capabilities (default: [HT40+]) + WiFi 4 (802.11n) configs: + --wifi4 Enable IEEE 802.11n (HT) + --req-ht Require station HT (High Throughput) mode + --ht-capab HT capabilities (default: [HT40+]) - WiFi 5(802.11AC) Config: - --ieee80211ac Enable IEEE 802.11ac (VHT) - --use_vht Enable Very High Thoughtput mode - --vht_capab VHT capabilities - --vht_channel_width - Index of VHT Channel Width: + WiFi 5 (802.11ac) configs: + --wifi5 Enable IEEE 802.11ac (VHT) + --req-vht Require station VHT (Very High Thoughtput) mode + --vht-capab VHT capabilities + + --vht-ch-width Index of VHT channel width: 0 for 20MHz or 40MHz (default) 1 for 80MHz 2 for 160MHz 3 for 80+80MHz (Non-contigous 160MHz) - --seg0_center_freq_idx - Channel index of Center frequency for primary segment, use with --vht_channel_width - --seg1_center_freq_idx - Channel index of Center frequency for secondary (second 80MHz) segment, use with --vht_channel_width=3 - Pick above 2 values from the F0 index column from the 5GHz table in https://en.wikipedia.org/wiki/List_of_WLAN_channels#5_GHz_(802.11a/h/n/ac/ax) - + --vht-seg0-ch Channel index of VHT center frequency for primary + segment. Use with '--vht-ch-width' + --vht-seg1-ch Channel index of VHT center frequency for secondary + (second 80MHz) segment. Use with '--vht-ch-width 3' + + WiFi 6 (802.11ax) configs: + --wifi6 Enable IEEE 802.11ax (HE) + --req-he Require station HE (High Efficiency) mode + + --he-ch-width Index of HE channel width: + 0 for 20MHz or 40MHz (default) + 1 for 80MHz + 2 for 160MHz + 3 for 80+80MHz (Non-contigous 160MHz) + --he-seg0-ch Channel index of HE center frequency for primary + segment. Use with '--he-ch-width' + --he-seg1-ch Channel index of HE center frequency for secondary + (second 80MHz) segment. Use with '--he-ch-width 3' + Instance managing: --daemon Run in background -l, --list-running Show running instances @@ -196,7 +208,6 @@ define_global_variables(){ HIDDEN=0 # hidden wifi hotspot WIFI_IFACE= CHANNEL=default - FORCECHANNEL=0 # Forces channel provided by -c flag HOTSPOT20=0 # For enabling Hotspot 2.0 WPA_VERSION=2 MAC_FILTER=0 @@ -205,11 +216,16 @@ define_global_variables(){ REQUIREHT=0 IEEE80211AC=0 REQUIREVHT=0 + IEEE80211AX=0 + REQUIRE_HE=0 HT_CAPAB='[HT40+]' VHT_CAPAB= VHTCHANNELWIDTH=0 VHTSEG0CHINDEX=0 VHTSEG1CHINDEX=0 + HECHANNELWIDTH=0 + HESEG0CHINDEX=0 + HESEG1CHINDEX=0 DRIVER=nl80211 NO_VIRT=0 # not use virtual interface COUNTRY= @@ -397,10 +413,6 @@ parse_user_options(){ CHANNEL="$1" shift ;; - --force_channel) - shift - FORCECHANNEL=1 - ;; --hs20) shift HOTSPOT20=1 @@ -412,47 +424,70 @@ parse_user_options(){ shift ;; - --ieee80211n) + --wifi4|--ieee80211n) shift IEEE80211N=1 ;; - --use_ht) + --req-ht|--require-ht) shift REQUIREHT=1 ;; - --ieee80211ac) + --wifi5|--ieee80211ac) shift IEEE80211AC=1 ;; - --use_vht) + --wifi6|--ieee80211ax) + shift + IEEE80211AX=1 + ;; + --req-he|--require-he) + shift + REQUIRE_HE=1 + ;; + --req-vht|--require-vht) shift REQUIREVHT=1 ;; - --ht_capab) + --ht-capab) shift HT_CAPAB="$1" shift ;; - --vht_capab) + --vht-capab) shift VHT_CAPAB="$1" shift ;; - --vht_channel_width) + --vht-ch-width|--vht-channel-width) shift VHTCHANNELWIDTH="$1" shift ;; - --seg0_center_freq_idx) + --vht-seg0-ch|--vht-seg0-channel) shift VHTSEG0CHINDEX="$1" shift ;; - --seg1_center_freq_idx) + --vht-seg1-ch|--vht-seg1-channel) shift VHTSEG1CHINDEX="$1" shift ;; + --he-ch-width|--he-channel-width) + shift + HECHANNELWIDTH="$1" + shift + ;; + --he-seg0-ch|--he-seg0-channel) + shift + HESEG0CHINDEX="$1" + shift + ;; + --he-seg1-ch|--he-seg1-channel) + shift + HESEG1CHINDEX="$1" + shift + ;; --driver) shift DRIVER="$1" @@ -484,9 +519,9 @@ parse_user_options(){ ;; --hostapd-debug) shift - if [ "x$1" = "x1" ]; then + if [ "$1" = "1" ]; then HOSTAPD_DEBUG_ARGS="-d" - elif [ "x$1" = "x2" ]; then + elif [ "$1" = "2" ]; then HOSTAPD_DEBUG_ARGS="-dd" else printf "Error: argument for --hostapd-debug expected 1 or 2, got %s\n" "$1" @@ -543,8 +578,8 @@ sep_ip_port() { if (echo "$INPUT" | grep '\.' >/dev/null 2>&1) ;then if (echo "$INPUT" | grep ':' >/dev/null 2>&1) ;then # ipv4 + port - IP="$(echo $INPUT | cut -d: -f1)" - PORT="$(echo $INPUT | cut -d: -f2)" + IP="$(echo "$INPUT" | cut -d: -f1)" + PORT="$(echo "$INPUT" | cut -d: -f2)" else # ipv4 IP="$INPUT" @@ -552,11 +587,11 @@ sep_ip_port() { elif (echo "$INPUT" | grep '\]' >/dev/null 2>&1) ;then if (echo "$INPUT" | grep '\]\:' >/dev/null 2>&1) ;then # ipv6 + port - IP="$(echo $INPUT | cut -d']' -f1 | cut -d'[' -f2)" - PORT="$(echo $INPUT | cut -d']' -f2 |cut -d: -f2)" + IP="$(echo "$INPUT" | cut -d']' -f1 | cut -d'[' -f2)" + PORT="$(echo "$INPUT" | cut -d']' -f2 |cut -d: -f2)" else # ipv6 - IP="$(echo $INPUT | cut -d']' -f1 | cut -d'[' -f2)" + IP="$(echo "$INPUT" | cut -d']' -f1 | cut -d'[' -f2)" fi else # port @@ -585,10 +620,10 @@ get_interface_phy_device() { # only for wifi interface echo "$1" return 0 elif [[ -e "$x/device/net/$1" ]]; then - echo ${x##*/} + echo "${x##*/}" return 0 elif [[ -e "$x/device/net:$1" ]]; then - echo ${x##*/} + echo "${x##*/}" return 0 fi done @@ -600,13 +635,13 @@ get_adapter_info() { # only for wifi interface local iPHY iPHY=$(get_interface_phy_device "$1") [[ $? -ne 0 ]] && return 1 - iw phy $iPHY info + iw phy "$iPHY" info } get_adapter_kernel_module() { local MODULE MODULE=$(readlink -f "/sys/class/net/$1/device/driver/module") - echo ${MODULE##*/} + echo "${MODULE##*/}" } can_be_sta_and_ap() { @@ -637,18 +672,14 @@ can_transmit_to_channel() { CHANNEL_NUM=$2 if [[ $USE_IWCONFIG -eq 0 ]]; then - if [[ $FREQ_BAND == 2.4 ]]; then - CHANNEL_INFO=$(get_adapter_info ${IFACE} | grep " 24[0-9][0-9] MHz \[${CHANNEL_NUM}\]") - else - CHANNEL_INFO=$(get_adapter_info ${IFACE} | grep " \(49[0-9][0-9]\|5[0-9]\{3\}\) MHz \[${CHANNEL_NUM}\]") - fi + CHANNEL_INFO=$(get_adapter_info "${IFACE}" | grep -E " [0-9]+(\.[0-9]+){0,1} MHz \[${CHANNEL_NUM}\]") [[ -z "${CHANNEL_INFO}" ]] && return 1 [[ "${CHANNEL_INFO}" == *no\ IR* ]] && return 1 [[ "${CHANNEL_INFO}" == *disabled* ]] && return 1 return 0 else CHANNEL_NUM=$(printf '%02d' ${CHANNEL_NUM}) - CHANNEL_INFO=$(iwlist ${IFACE} channel | grep -E "Channel[[:blank:]]${CHANNEL_NUM}[[:blank:]]?:") + CHANNEL_INFO=$(iwlist "${IFACE}" channel | grep -E "Channel[[:blank:]]${CHANNEL_NUM}[[:blank:]]?:") [[ -z "${CHANNEL_INFO}" ]] && return 1 return 0 fi @@ -672,9 +703,6 @@ ieee80211_frequency_to_channel() { fi } -is_5ghz_frequency() { - [[ $1 =~ ^(49[0-9]{2})|(5[0-9]{3})$ ]] -} is_interface_wifi_connected() { if [[ $USE_IWCONFIG -eq 0 ]]; then @@ -698,29 +726,42 @@ get_interface_mac() { cat "/sys/class/net/${1}/address" } -get_interface_pci_info() { # pci id / model / virtual +show_interface_pci_info() { # pci id / model / virtual is_interface "$1" || return local device_path - local pci_id - local pci_full + local bus_id="" + local device_type_and_bus_id="unknown" + local driver="" + local device_fullname="" device_path="$(readlink -f /sys/class/net/$1)" if [[ "$device_path" == "/sys/devices/pci"* ]]; then - pci_id="$(echo $device_path | sed 's/\//\n/g' | tail -n 3 |sed -n 1p)" + local pci_path + + pci_path=$device_path/../.. + + if [[ -d "$pci_path/driver" ]] ; then + driver=$(readlink -f "$pci_path/driver" | sed 's/\//\n/g' | tail -n 1) + fi + + bus_id="$(echo "$device_path" | sed 's/\//\n/g' | tail -n 3 |sed -n 1p)" + device_type_and_bus_id="PCI: $bus_id" if which lspci >/dev/null 2>&1 ; then - pci_full="$( lspci -D -nn | grep -E "^$pci_id " )" - echo " PCI: $pci_full" - else - echo " PCI: $pci_id" + device_fullname="$( lspci -D -nn -s "$bus_id" | awk '{$1="" ; print $0}' )" fi + elif [[ "$device_path" == *"/virtual/"* ]]; then - echo " virtual interface" + device_type_and_bus_id="virtual interface" fi + + echo "$device_type_and_bus_id" + [[ -n "$driver" ]] && echo "System-already-loaded driver: $driver" + [[ -n "$device_fullname" ]] && echo "$device_fullname" + echo "" # TODO usb - # TODO current driver } alloc_new_vface_name() { # only for wifi @@ -730,16 +771,16 @@ alloc_new_vface_name() { # only for wifi while :; do v_iface_name="x$i${WIFI_IFACE}" i=$((i + 1)) - is_vface_name_allocated ${v_iface_name} || break + is_vface_name_allocated "${v_iface_name}" || break done fi - mkdir -p $COMMON_CONFDIR/vfaces - touch $COMMON_CONFDIR/vfaces/${v_iface_name} + mkdir -p "$COMMON_CONFDIR/vfaces" + touch "$COMMON_CONFDIR/vfaces/${v_iface_name}" echo "${v_iface_name}" } dealloc_vface_name() { - rm -f $COMMON_CONFDIR/vfaces/$1 + rm -f "$COMMON_CONFDIR/vfaces/$1" } #====== @@ -772,10 +813,10 @@ generate_random_mac() { r5=$( printf "%02x" $(($RANDOM%256)) ) r6=$( printf "%02x" $(($RANDOM%256)) ) RAND_MAC="$r1:$r2:$r3:$r4:$r5:$r6" - ( ! ip link | grep "link" | grep $RAND_MAC > /dev/null 2>&1 ) && \ - ( ! ip maddress | grep "link" | grep $RAND_MAC > /dev/null 2>&1 ) && \ + ( ! ip link | grep "link" | grep "$RAND_MAC" > /dev/null 2>&1 ) && \ + ( ! ip maddress | grep "link" | grep "$RAND_MAC" > /dev/null 2>&1 ) && \ ( ! ip neigh | grep "lladdr $RAND_MAC" > /dev/null 2>&1 ) && \ - ( ! get_all_mac_in_system | grep $RAND_MAC ) && \ + ( ! get_all_mac_in_system | grep "$RAND_MAC" ) && \ break done echo "$RAND_MAC" @@ -815,7 +856,7 @@ generate_random_lan_ip6_prefix() { r5=$( printf "%x" $(($RANDOM%240+16)) ) r6=$( printf "%x" $(($RANDOM%240+16)) ) r7=$( printf "%x" $(($RANDOM%240+16)) ) - is_ip6_lan_range_available $r1 $r2 $r3 $r4 $r5 $r6 $r7 && break + is_ip6_lan_range_available "$r1" "$r2" "$r3" "$r4" "$r5" "$r6" "$r7" && break done echo "fd$r1:$r2$r3:$r4$r5:$r6$r7::" } @@ -851,7 +892,7 @@ pid_watchdog() { if [[ -e "/proc/$PID" ]]; then ST="$(cat "/proc/$PID/status" | grep "^State:" | awk '{print $2}')" if [[ "$ST" != 'Z' ]]; then - sleep $SLEEP + sleep "$SLEEP" continue fi fi @@ -872,35 +913,35 @@ is_nm_running() { } nm_knows() { - (nmcli dev show $1 | grep -E "^GENERAL.STATE:" >/dev/null 2>&1 ) && return 0 # nm sees + (nmcli dev show "$1" | grep -E "^GENERAL.STATE:" >/dev/null 2>&1 ) && return 0 # nm sees return 1 # nm doesn't see this interface } nm_get_manage() { # get an interface's managed state local s - s=$(nmcli dev show $1 | grep -E "^GENERAL.STATE:") || return 2 # no such interface - (echo $s | grep "unmanaged" >/dev/null 2>&1) && return 1 # unmanaged + s=$(nmcli dev show "$1" | grep -E "^GENERAL.STATE:") || return 2 # no such interface + (echo "$s" | grep "unmanaged" >/dev/null 2>&1) && return 1 # unmanaged return 0 # managed } nm_set_unmanaged() { - while ! nm_knows $1 ; do # wait for virtual wifi interface seen by NM + while ! nm_knows "$1" ; do # wait for virtual wifi interface seen by NM sleep 0.5 done - if nm_get_manage $1 ;then + if nm_get_manage "$1" ;then echo "Set $1 unmanaged by NetworkManager" - nmcli dev set $1 managed no || die "Failed to set $1 unmanaged by NetworkManager" + nmcli dev set "$1" managed no || die "Failed to set $1 unmanaged by NetworkManager" NM_UNM_LIST=$1 sleep 1 fi } nm_set_managed() { - nmcli dev set $1 managed yes + nmcli dev set "$1" managed yes NM_UNM_LIST= } nm_restore_manage() { if [[ $NM_UNM_LIST ]]; then echo "Restore $NM_UNM_LIST managed by NetworkManager" - nm_set_managed $NM_UNM_LIST + nm_set_managed "$NM_UNM_LIST" sleep 0.5 fi } @@ -1168,18 +1209,18 @@ set_interface_mac() { INTERFACE=$1 MAC=$2 - ip link set dev ${INTERFACE} address ${MAC} + ip link set dev "${INTERFACE}" address "${MAC}" } backup_interface_status() { # virtual wifi interface will be destroyed, so no need to save status # backup interface up or down status - (ip link show ${SUBNET_IFACE} |grep -q "state UP") && SUBNET_IFACE_ORIGINAL_UP_STATUS=1 + (ip link show "${SUBNET_IFACE}" |grep -q "state UP") && SUBNET_IFACE_ORIGINAL_UP_STATUS=1 # save interface old mac #if [[ -n "$NEW_MACADDR" ]]; then - OLD_MACADDR=$(get_interface_mac $SUBNET_IFACE) + OLD_MACADDR=$(get_interface_mac "$SUBNET_IFACE") #echo "Saved ${SUBNET_IFACE} old MAC address ${OLD_MACADDR} into RAM" #fi @@ -1196,14 +1237,14 @@ restore_interface_status() { restore_ipv6_bits - if [[ -n "$OLD_MACADDR" && "$(get_interface_mac $SUBNET_IFACE)" != "$OLD_MACADDR" ]] ; then + if [[ -n "$OLD_MACADDR" && "$(get_interface_mac "$SUBNET_IFACE")" != "$OLD_MACADDR" ]] ; then echo "Restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR} ..." - set_interface_mac ${SUBNET_IFACE} ${OLD_MACADDR} || echo "Failed restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR}" >&2 + set_interface_mac "${SUBNET_IFACE}" "${OLD_MACADDR}" || echo "Failed restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR}" >&2 fi nm_restore_manage - [[ $SUBNET_IFACE_ORIGINAL_UP_STATUS -eq 1 ]] && ip link set up dev ${SUBNET_IFACE} && echo "Restore ${SUBNET_IFACE} to link up" + [[ $SUBNET_IFACE_ORIGINAL_UP_STATUS -eq 1 ]] && ip link set up dev "${SUBNET_IFACE}" && echo "Restore ${SUBNET_IFACE} to link up" } #--------------------------------------- @@ -1214,11 +1255,11 @@ kill_processes() { # for this instance # even if the $CONFDIR is empty, the for loop will assign # a value in $x. so we need to check if the value is a file if [[ -f $x ]] && sleep 0.3 && [[ -f $x ]]; then - pid=$(cat $x) - pn=$( ps -p $pid -o comm= ) + pid=$(cat "$x") + pn=$( ps -p "$pid" -o comm= ) #echo "Killing $pid $pn ... " - pkill -P $pid - kill $pid 2>/dev/null && ( echo "Killed $(basename $x) $pid $pn" && rm $x ) || echo "Failed to kill $(basename $x) $pid $pn, it may have exited" + pkill -P "$pid" + kill "$pid" 2>/dev/null && ( echo "Killed $(basename "$x") $pid $pn" && rm "$x" ) || echo "Failed to kill $(basename "$x") $pid $pn, it may have exited" fi done } @@ -1226,15 +1267,15 @@ kill_processes() { # for this instance _cleanup() { local x - ip addr flush ${SUBNET_IFACE} + ip addr flush "${SUBNET_IFACE}" - rm -rf $CONFDIR + rm -rf "$CONFDIR" - ip link set down dev ${SUBNET_IFACE} + ip link set down dev "${SUBNET_IFACE}" if [[ $VWIFI_IFACE ]]; then # the subnet interface (virtual wifi interface) will be removed - iw dev ${VWIFI_IFACE} del - dealloc_vface_name $VWIFI_IFACE + iw dev "${VWIFI_IFACE}" del + dealloc_vface_name "$VWIFI_IFACE" fi restore_interface_status @@ -1243,12 +1284,12 @@ _cleanup() { echo "Exiting: This is the only running instance" # kill common processes for x in $COMMON_CONFDIR/*.pid; do - [[ -f $x ]] && kill -9 $(cat $x) && rm $x + [[ -f $x ]] && kill -9 $(cat "$x") && rm "$x" done - rm -d $COMMON_CONFDIR/vfaces - rm -d $COMMON_CONFDIR - rm -d $TMPDIR + rm -d "$COMMON_CONFDIR/vfaces" + rm -d "$COMMON_CONFDIR" + rm -d "$TMPDIR" else echo "Exiting: This is NOT the only running instance" fi @@ -1330,7 +1371,7 @@ list_running() { IFACE=${IFACE%%.*} subn_iface=$(cat $x/subn_iface) - if [[ $IFACE == $subn_iface ]]; then + if [[ "$IFACE" == "$subn_iface" ]]; then echo $(cat $x/pid) $IFACE else echo $(cat $x/pid) $IFACE '('$(cat $x/subn_iface)')' @@ -1367,7 +1408,7 @@ print_clients_from_leases() { # MAC|IP|HOST|lease FILEC="$(cat "$LEASE_FILE" | grep -v -E "^duid\b" | sed -r '/^\s*$/d' )" # TODO: duid is somewhat related to ipv6. I don't know about it. Not sure excluding it miss some info or not - echo "$FILEC" | while read line + echo "$FILEC" | while read -r line do #echo aa$line LEASEstamp="$(echo "$line" | awk '{print $1}')" @@ -1388,20 +1429,20 @@ print_interface_neighbors_via_iproute() { # MAC|IP|_|STATUS local line - ip n | grep -E "\bdev $IFACE\b" | sed 's/ /|/g' | while read line + ip n | grep -E "\bdev $IFACE\b" | sed 's/ /|/g' | while read -r line do local MAC IP STATUS - IP="$(echo $line | awk -F'|' '{print $1}')" + IP="$(echo "$line" | awk -F'|' '{print $1}')" - if [[ "$(echo $line | awk -F'|' '{print $4}')" == "lladdr" ]]; then # has mac + if [[ "$(echo "$line" | awk -F'|' '{print $4}')" == "lladdr" ]]; then # has mac # if has mac, $4="lladdr" and $5=macaddress and $6+=status - MAC="$(echo $line | awk -F'|' '{print $5}')" - STATUS="$(echo $line | awk -F'|' '$1="";$2="";$3="";$4="";$5="";{print}' | awk '{$1=$1;print}'| sed 's/ /,/g')" + MAC="$(echo "$line" | awk -F'|' '{print $5}')" + STATUS="$(echo "$line" | awk -F'|' '$1="";$2="";$3="";$4="";$5="";{print}' | awk '{$1=$1;print}'| sed 's/ /,/g')" else # no mac # if no mac, $4="" and $5+=status MAC="?" - STATUS="$(echo $line | awk -F'|' '$1="";$2="";$3="";$4="";{print}' | awk '{$1=$1;print}' | sed 's/ /,/g')" + STATUS="$(echo "$line" | awk -F'|' '$1="";$2="";$3="";$4="";{print}' | awk '{$1=$1;print}' | sed 's/ /,/g')" fi if [[ -n "$IP" && ( "$MAC" != "?" || "$STATUS" != "FAILED" ) ]]; then echo "$MAC|$IP|?|$STATUS" @@ -1411,10 +1452,10 @@ print_interface_neighbors_via_iproute() { # MAC|IP|_|STATUS print_interface_neighbors_via_iw() { # MAC|_|_|signal local IFACE=$1 local MAC SIGNAL - iw dev $IFACE station dump | awk '($1 ~ /Station$/) {print $2}' | while read MAC + iw dev "$IFACE" station dump | awk '($1 ~ /Station$/) {print $2}' | while read -r MAC do if [[ -n "$MAC" ]]; then - SIGNAL="$(iw dev $IFACE station get $MAC | grep "signal:" | awk '{print $2}')" + SIGNAL="$(iw dev "$IFACE" station get "$MAC" | grep "signal:" | awk '{print $2}')" echo "${MAC}|?|?|${SIGNAL}_dBm" fi done @@ -1435,7 +1476,7 @@ list_clients() { # passive mode. (use 'arp-scan' or 'netdiscover' if want active fi else # non-number given IFACE="$1" - if ( ! is_interface $IFACE ) ; then + if ( ! is_interface "$IFACE" ) ; then echo "'$IFACE' is not an interface or PID" >&2 exit 1 fi @@ -1447,18 +1488,18 @@ list_clients() { # passive mode. (use 'arp-scan' or 'netdiscover' if want active echo "Tip: '$IFACE' is not an interface hosted by $PROGNAME" >&2 fi fi - output="$(echo "$output" ; print_interface_neighbors_via_iw $IFACE) " - output="$(echo "$output" ; print_interface_neighbors_via_iproute $IFACE)" + output="$(echo "$output" ; print_interface_neighbors_via_iw "$IFACE") " + output="$(echo "$output" ; print_interface_neighbors_via_iproute "$IFACE")" output="$(echo "$output" | sort -k 1 -k 2 -t '|' | uniq | sed -r '/^\s*$/d')" - echo "$IFACE ($(get_interface_mac $IFACE)) neighbors:" + echo "$IFACE ($(get_interface_mac "$IFACE")) neighbors:" local fmt="%-19s%-41s%-20s%s" # string length: MAC 17, ipv4 15, ipv6 39, hostname ? printf "$fmt\n" "MAC" "IP" "HOSTNAME" "INFO" local line - echo "$output"| while read line + echo "$output"| while read -r line do if [[ -n "$line" ]]; then echo "$line" | awk -F'|' "{printf \"$fmt\n\",\$1,\$2,\$3,\$4}" @@ -1490,14 +1531,14 @@ send_stop() { local x # send stop signal to specific pid - if is_running_pid $1; then - kill -USR1 $1 + if is_running_pid "$1"; then + kill -USR1 "$1" return fi # send stop signal to specific interface for x in $(list_running | grep -E " \(?${1}( |\)?\$)" | cut -f1 -d' '); do - kill -USR1 $x + kill -USR1 "$x" done } @@ -1560,13 +1601,13 @@ daemonizing_check(){ #============================ check_wifi_settings() { - if ! ( which iw > /dev/null 2>&1 && iw dev $WIFI_IFACE info > /dev/null 2>&1 ); then - echo "WARN: Can't use 'iw' to operate interfce '$WIFI_IFACE', trying 'iwconfig' (not as good as 'iw') ..." >&2 + if ! ( which iw > /dev/null 2>&1 && iw dev "$WIFI_IFACE" info > /dev/null 2>&1 ); then + echo "WARN: Can't use 'iw' to operate interfce '$WIFI_IFACE', trying 'iwconfig' (not as good as 'iw') ... (Did you spell the interface name right?)" >&2 USE_IWCONFIG=1 fi if [[ $USE_IWCONFIG -eq 1 ]]; then - if ! (which iwconfig > /dev/null 2>&1 && iwconfig $WIFI_IFACE > /dev/null 2>&1); then + if ! (which iwconfig > /dev/null 2>&1 && iwconfig "$WIFI_IFACE" > /dev/null 2>&1); then echo "ERROR: Can't use 'iwconfig' to operate interfce '$WIFI_IFACE'" >&2 exit 1 fi @@ -1577,26 +1618,18 @@ check_wifi_settings() { exit 1 fi - if [[ $CHANNEL == default ]]; then - if [[ $FREQ_BAND == 2.4 ]]; then - CHANNEL=1 - else - CHANNEL=36 - fi - fi - if [[ $FREQ_BAND != 5 && $CHANNEL -gt 14 ]]; then echo "Channel number is greater than 14, assuming 5GHz frequency band" FREQ_BAND=5 fi - if ! can_be_ap ${WIFI_IFACE}; then + if ! can_be_ap "${WIFI_IFACE}"; then echo "ERROR: Your adapter does not support AP (master) mode" >&2 exit 1 fi - if ! can_be_sta_and_ap ${WIFI_IFACE}; then - if is_interface_wifi_connected ${WIFI_IFACE}; then + if ! can_be_sta_and_ap "${WIFI_IFACE}"; then + if is_interface_wifi_connected "${WIFI_IFACE}"; then echo "ERROR: Your adapter can not be a station (i.e. be connected) and an AP at the same time" >&2 exit 1 elif [[ $NO_VIRT -eq 0 ]]; then @@ -1607,7 +1640,7 @@ check_wifi_settings() { HOSTAPD=$(which hostapd) - if [[ $(get_adapter_kernel_module ${WIFI_IFACE}) =~ ^(8192[cd][ue]|8723a[sue])$ ]]; then + if [[ $(get_adapter_kernel_module "${WIFI_IFACE}") =~ ^(8192[cd][ue]|8723a[sue])$ ]]; then if ! strings "$HOSTAPD" | grep -m1 rtl871xdrv > /dev/null 2>&1; then echo "ERROR: You need to patch your hostapd with rtl871xdrv patches." >&2 exit 1 @@ -1634,7 +1667,7 @@ check_wifi_settings() { exit 1 fi - if [[ $(get_adapter_kernel_module ${WIFI_IFACE}) =~ ^rtl[0-9].*$ ]]; then + if [[ $(get_adapter_kernel_module "${WIFI_IFACE}") =~ ^rtl[0-9].*$ ]]; then if [[ $WPA_VERSION == '1' || $WPA_VERSION == '1+2' ]]; then echo "WARN: Realtek drivers usually have problems with WPA1, WPA2 is recommended" >&2 fi @@ -1649,7 +1682,7 @@ check_wifi_settings() { echo "WARN: option --virt-name $VIRT_NAME has ${#VIRT_NAME} characters which might be too long, consider making it shorter in case of errors" >&2 fi - if [[ ! -z $VIRT_NAME ]] && is_vface_name_allocated $VIRT_NAME; then + if [[ ! -z $VIRT_NAME ]] && is_vface_name_allocated "$VIRT_NAME"; then echo "WARN: interface $VIRT_NAME aleady exists, this will cause an error" fi } @@ -1660,7 +1693,7 @@ check_if_new_mac_valid() { exit 1 fi - if [[ $(get_all_mac_in_system | grep -c ${NEW_MACADDR}) -ne 0 ]]; then + if [[ $(get_all_mac_in_system | grep -c "${NEW_MACADDR}") -ne 0 ]]; then echo "WARN: MAC address '${NEW_MACADDR}' already exists" >&2 fi } @@ -1698,31 +1731,32 @@ decide_ip_addresses() { prepare_wifi_interface() { if [[ $USE_IWCONFIG -eq 0 ]]; then - iw dev ${WIFI_IFACE} set power_save off + iw dev "${WIFI_IFACE}" set power_save off fi if [[ $NO_VIRT -eq 0 ]]; then ## Will generate virtual wifi interface - if is_interface_wifi_connected ${WIFI_IFACE} && [[ FORCECHANNEL -eq 0 ]]; then - WIFI_IFACE_FREQ=$(iw dev ${WIFI_IFACE} link | grep -i freq | awk '{print $2}') - WIFI_IFACE_CHANNEL=$(ieee80211_frequency_to_channel ${WIFI_IFACE_FREQ}) - echo "${WIFI_IFACE} already in channel ${WIFI_IFACE_CHANNEL} (${WIFI_IFACE_FREQ} MHz)" - if is_5ghz_frequency $WIFI_IFACE_FREQ; then - FREQ_BAND=5 - else - FREQ_BAND=2.4 - fi - if [[ $WIFI_IFACE_CHANNEL -ne $CHANNEL ]]; then - echo "Channel fallback to ${WIFI_IFACE_CHANNEL}" + + # TODO move this to check_wifi_settings() ? + if is_interface_wifi_connected "${WIFI_IFACE}"; then + WIFI_IFACE_FREQ=$(iw dev "${WIFI_IFACE}" link | grep -i freq | awk '{print $2}' | sed 's/\.00*$//g') # NOTE we assume integer currently, which can be right, or wrong in the future + WIFI_IFACE_CHANNEL=$(ieee80211_frequency_to_channel "${WIFI_IFACE_FREQ}") + + echo "${WIFI_IFACE} already working in channel ${WIFI_IFACE_CHANNEL} (${WIFI_IFACE_FREQ} MHz)" + + if [[ $CHANNEL == default ]]; then + echo "Use wifi adapter current channel $WIFI_IFACE_CHANNEL as target channel" CHANNEL=$WIFI_IFACE_CHANNEL - else - echo + fi + + if [[ $WIFI_IFACE_CHANNEL -ne $CHANNEL ]]; then + echo "WARN: Wifi adapter already working in channel ${WIFI_IFACE_CHANNEL}, which is different than target channel $CHANNEL" >&2 fi fi echo "Creating a virtual WiFi interface... " VWIFI_IFACE=$(alloc_new_vface_name) - if iw dev ${WIFI_IFACE} interface add ${VWIFI_IFACE} type __ap; then + if iw dev "${WIFI_IFACE}" interface add "${VWIFI_IFACE}" type __ap; then # Successfully created virtual wifi interface # if NM running, it will give the new virtual interface a random MAC. MAC will go back after setting NM unmanaged sleep 2 @@ -1742,6 +1776,15 @@ prepare_wifi_interface() { else # no virtual wifi interface, use wifi device interface itself AP_IFACE=${WIFI_IFACE} fi + + if [[ $CHANNEL == default ]]; then + echo "Channel not specified, use default" + if [[ $FREQ_BAND == 2.4 ]]; then + CHANNEL=1 + else + CHANNEL=36 + fi + fi } decide_subnet_interface() { @@ -1758,14 +1801,14 @@ dealwith_mac() { if [[ -n "$NEW_MACADDR" ]] ; then # user choose to set subnet mac echo "Setting ${SUBNET_IFACE} new MAC address ${NEW_MACADDR} ..." - set_interface_mac ${SUBNET_IFACE} ${NEW_MACADDR} || die "Failed setting new MAC address" + set_interface_mac "${SUBNET_IFACE}" "${NEW_MACADDR}" || die "Failed setting new MAC address" elif [[ $VWIFI_IFACE ]]; then # user didn't choose to set mac, but using virtual wifi interface - VMAC=$(get_new_macaddr_according_to_existing ${WIFI_IFACE}) + VMAC=$(get_new_macaddr_according_to_existing "${WIFI_IFACE}") if [[ "$VMAC" ]]; then echo "Assigning MAC address $VMAC to virtual interface $VWIFI_IFACE according to $WIFI_IFACE ..." - set_interface_mac $VWIFI_IFACE $VMAC + set_interface_mac "$VWIFI_IFACE" "$VMAC" fi fi } @@ -1826,6 +1869,15 @@ write_hostapd_conf() { echo "require_vht=1" >> "$CONFDIR/hostapd.conf" fi + if [[ $IEEE80211AX -eq 1 ]]; then + echo "ieee80211ax=1" >> "$CONFDIR/hostapd.conf" + fi + + if [[ $REQUIRE_HE -eq 1 ]]; then + echo "require_he=1" >> "$CONFDIR/hostapd.conf" + fi + + if [[ -n "$VHT_CAPAB" ]]; then echo "vht_capab=${VHT_CAPAB}" >> "$CONFDIR/hostapd.conf" fi @@ -1848,6 +1900,24 @@ write_hostapd_conf() { EOF fi + if [[ $HECHANNELWIDTH -gt 0 ]]; then + cat <<- EOF >> "$CONFDIR/hostapd.conf" + he_oper_chwidth=${HECHANNELWIDTH} + EOF + fi + + if [[ $HESEG0CHINDEX -gt 0 ]]; then + cat <<- EOF >> "$CONFDIR/hostapd.conf" + he_oper_centr_freq_seg0_idx=${HESEG0CHINDEX} + EOF + fi + + if [[ $HESEG1CHINDEX -gt 0 ]]; then + cat <<- EOF >> "$CONFDIR/hostapd.conf" + he_oper_centr_freq_seg1_idx=${HESEG1CHINDEX} + EOF + fi + if [[ $IEEE80211N -eq 1 ]] || [[ $IEEE80211AC -eq 1 ]]; then echo "wmm_enabled=1" >> "$CONFDIR/hostapd.conf" fi @@ -1869,7 +1939,6 @@ write_hostapd_conf() { else echo "WARN: WiFi is not protected by password" >&2 fi - echo "Config for current session is $CONFDIR/hostapd.conf" # Useful for sharing with other hostapd users. chmod 600 "$CONFDIR/hostapd.conf" } @@ -1932,7 +2001,7 @@ write_dnsmasq_conf() { if [[ $DNS ]]; then DNS_count=$(echo "$DNS" | awk -F, '{print NF}') for (( i=1;i<=DNS_count;i++ )); do - sep_ip_port "$(echo $DNS | cut -d, -f$i)" DNS_IP DNS_PORT + sep_ip_port "$(echo "$DNS" | cut -d, -f$i)" DNS_IP DNS_PORT [[ "$DNS_PORT" ]] && DNS_PORT_D="#$DNS_PORT" echo "server=${DNS_IP}${DNS_PORT_D}" >> "$CONFDIR/dnsmasq.conf" done @@ -1976,7 +2045,7 @@ run_wifi_ap_processes() { # start access point #echo "hostapd command-line interface: hostapd_cli -p $CONFDIR/hostapd_ctrl" # start hostapd (use stdbuf when available for no delayed output in programs that redirect stdout) - STDBUF_PATH=`which stdbuf` + STDBUF_PATH=$(which stdbuf) if [ $? -eq 0 ]; then STDBUF_PATH=$STDBUF_PATH" -oL" fi @@ -1996,7 +2065,7 @@ run_wifi_ap_processes() { # sleep 1 #done #echo -n "hostapd PID: " ; cat $CONFDIR/hostapd.pid - pid_watchdog $HOSTAPD_PID 10 "hostapd failed" & + pid_watchdog "$HOSTAPD_PID" 10 "hostapd failed. (tip: try '--hostapd-debug' to get some debug info)" & sleep 3 } @@ -2023,15 +2092,15 @@ start_dnsmasq() { DNSMASQ_PID="$(cat "$CONFDIR/dnsmasq.pid" )" echo "dnsmasq PID: $DNSMASQ_PID" ######(wait $DNSMASQ_PID ; die "dnsmasq failed") & # wait can't deal with non-child - pid_watchdog $DNSMASQ_PID 9 "dnsmasq failed" & + pid_watchdog "$DNSMASQ_PID" 9 "dnsmasq failed" & sleep 2 } check_rfkill_unblock_wifi() { local PHY if which rfkill > /dev/null 2>&1 ; then - PHY=$(get_interface_phy_device ${SUBNET_IFACE}) - [[ -n $PHY ]] && rfkill unblock $(rfkill | grep $PHY | awk '{print $1}') >/dev/null 2>&1 + PHY=$(get_interface_phy_device "${SUBNET_IFACE}") + [[ -n $PHY ]] && rfkill unblock $(rfkill | grep "$PHY" | awk '{print $1}') >/dev/null 2>&1 fi } @@ -2078,7 +2147,8 @@ echo echo "PID: $$" TARGET_IFACE="$(decide_target_interface)" || exit 1 # judge wired (-i CONN_IFACE) or wireless hotspot (--ap $WIFI_IFACE) -echo "Target interface is ${TARGET_IFACE} ($(get_interface_mac $TARGET_IFACE)) $(get_interface_pci_info $TARGET_IFACE)" +echo "Target interface is ${TARGET_IFACE} ($(get_interface_mac "$TARGET_IFACE")) " +show_interface_pci_info "$TARGET_IFACE" if [[ "$MAC_USE_RANDOM" -eq 1 ]] ; then NEW_MACADDR="$(generate_random_mac)" @@ -2114,7 +2184,7 @@ fi # judge channel availability after changing country code if [[ $WIFI_IFACE ]] ; then - can_transmit_to_channel ${AP_IFACE} ${CHANNEL} || die "Your adapter can not transmit to channel ${CHANNEL}, frequency band ${FREQ_BAND}GHz." + can_transmit_to_channel "${AP_IFACE}" ${CHANNEL} || die "Your adapter can not transmit to channel ${CHANNEL}, frequency band ${FREQ_BAND}GHz." fi [[ $WIFI_IFACE ]] && write_hostapd_conf @@ -2122,8 +2192,8 @@ fi #=================================================== # set interface unmanaged by networkManager -if [[ $NM_RUNNING -eq 1 ]] && nm_knows $TARGET_IFACE; then # if nm knows target iface, should know subnet iface too. but need to wait until nm finds subnet iface (waiting code is in nm_set_unmanaged() - nm_set_unmanaged ${SUBNET_IFACE} # will write NM_UNM_LIST +if [[ $NM_RUNNING -eq 1 ]] && nm_knows "$TARGET_IFACE"; then # if nm knows target iface, should know subnet iface too. but need to wait until nm finds subnet iface (waiting code is in nm_set_unmanaged() + nm_set_unmanaged "${SUBNET_IFACE}" # will write NM_UNM_LIST fi [[ $NO_DNSMASQ -eq 0 ]] && write_dnsmasq_conf @@ -2131,16 +2201,16 @@ fi # initialize subnet interface # take subnet interface down first -ip link set down dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} down" +ip link set down dev "${SUBNET_IFACE}" || die "Failed setting ${SUBNET_IFACE} down" # flush old IPs of subnet interface -ip addr flush ${SUBNET_IFACE} || die "Failed flush ${SUBNET_IFACE} IP" +ip addr flush "${SUBNET_IFACE}" || die "Failed flush ${SUBNET_IFACE} IP" dealwith_mac # setting MAC should be after setting NM unmanaged [[ $WIFI_IFACE ]] && check_rfkill_unblock_wifi # bring subnet interface up -ip link set up dev ${SUBNET_IFACE} || die "Failed bringing ${SUBNET_IFACE} up" +ip link set up dev "${SUBNET_IFACE}" || die "Failed bringing ${SUBNET_IFACE} up" # hostapd , haveged [[ $WIFI_IFACE ]] && run_wifi_ap_processes @@ -2243,6 +2313,6 @@ show_qr() { bash -c "while :; do sleep 8000 ; done " & KEEP_RUNNING_PID=$! echo "$KEEP_RUNNING_PID" > "$CONFDIR/keep_running.pid" -wait $KEEP_RUNNING_PID +wait "$KEEP_RUNNING_PID" clean_exit