]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.phy
hostapd: Disable DFS automatically when not supported by hardware
[people/ms/network.git] / src / functions / functions.phy
CommitLineData
590cb657
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22PHY_DIR="/sys/class/ieee80211"
23
1c6a4e30 24phy_dir() {
590cb657
MT
25 local phy=${1}
26
27 echo "${PHY_DIR}/${phy}"
28}
29
1c6a4e30 30phy_exists() {
590cb657
MT
31 local phy=${1}
32 assert isset phy
33
34 [ -d "$(phy_dir ${phy})" ]
35}
36
1c6a4e30 37phy_list() {
60b1f378 38 list_directory "$(phy_dir)"
590cb657
MT
39}
40
1c6a4e30 41phy_get() {
4733a336 42 local info="${1}"
590cb657
MT
43 local phy
44
4733a336
MT
45 # As this is already a valid phy, we don't need to search on...
46 if phy_exists "${info}"; then
47 print "${info}"
48 return ${EXIT_OK}
590cb657
MT
49 fi
50
4733a336
MT
51 # If this is an existing device, we can figure out the name of the
52 # phy directly.
53 if device_exists ${info}; then
54 phy="$(device_get_phy ${info})"
55
56 # If we just got the MAC address we need to walk though all
57 # available phys and find the right one.
58 elif mac_is_valid ${info}; then
590cb657
MT
59 local i
60 for i in $(phy_list); do
61 if [ "${info}" = "$(phy_get_address ${i})" ]; then
62 phy=${i}
63 break
64 fi
65 done
66 fi
67
31670741
MT
68 log DEBUG "Searching for phy = ${info}, found ${phy:-none}"
69
590cb657
MT
70 if [ -z "${phy}" ]; then
71 return ${EXIT_ERROR}
72 fi
73
74 echo "${phy}"
75 return ${EXIT_OK}
76}
77
1c6a4e30 78phy_get_address() {
590cb657
MT
79 local phy=${1}
80 assert isset phy
81
82 local path="$(phy_dir ${phy})/macaddress"
83 [ -r "${path}" ] || return ${EXIT_ERROR}
84
85 print "$(<${path})"
86}
b8026986 87
4ad5981f
MT
88phy_get_driver() {
89 local phy="${1}"
90 assert isset phy
91
92 get_driver_from_path "$(phy_dir "${phy}")/device/driver/module"
93}
94
1c6a4e30 95phy_get_devices() {
b8026986
MT
96 local phy="${1}"
97 assert isset phy
98
99 local device
91aeb1be 100 for device in $(device_list); do
b8026986
MT
101 local p="$(device_get_phy "${device}")"
102
103 if [ "${phy}" = "${p}" ]; then
104 print "${device}"
105 fi
106 done
107}
208f7452
MT
108
109phy_list_leds() {
110 local phy="${1}"
111
112 # Check if the PHY exists
113 assert phy_exists "${phy}"
114
115 local led
116 for led in $(list_directory /sys/class/leds); do
117 # Get basename of the LED
118 led=${led%*/}
119
120 if [[ ${led} =~ ${phy}(:.*)?$ ]]; then
121 print "${led}"
122 fi
123 done
124}
125
126# This function tries to automatically configure LEDs to
127# something useful
128phy_leds_autoconf() {
129 local phy="${1}"
130 assert isset phy
131
132 local led
133 for led in $(phy_list_leds "${phy}"); do
134 # Skip some types of LEDs
135 case "${led}" in
136 # Pretty much everything we tested from Ralink
137 # locked up the kernel after a couple of seconds
138 rt*)
139 continue
140 ;;
141 esac
142
143 # We try to set the LED into tpt mode (flashing on activity),
144 # but will fallback to tx mode if that isn't supported
145 local trigger
146 for trigger in "${phy}tpt" "${phy}tx"; do
147 if led_set_trigger "${led}" "${trigger}"; then
148 break
149 fi
150 done
151 done
152
153 return ${EXIT_OK}
154}
394cff53
MT
155
156phy_supports_channel() {
157 local phy="${1}"
158 assert isset phy
159
160 local channel="${2}"
161 assert isinteger channel
162
163 local _channel _frequency _dfs _max_tx_power
164 while read -r _channel _frequency _dfs _max_tx_power; do
165 if [ "${channel}" = "${_channel}" ]; then
166 return ${EXIT_TRUE}
167 fi
168 done <<< "$(network-phy-list-channels "${phy}")"
169
170 return ${EXIT_FALSE}
171}
109884be
MT
172
173__phy_list_ht_capabilities() {
174 local phy="${1}"
175 assert isset phy
176
177 local capabilities="$(network-phy-list-ht-caps "${phy}")"
178
179 print "${capabilities//[\[\]]/ }"
180}
181
182phy_supports_ht_capability() {
183 local phy="${1}"
184 assert isset phy
185
186 local capability="${2}"
187 assert isset capability
188
189 list_match "${capability}" $(__phy_list_ht_capabilities "${phy}")
190}
dc6d97fb
MT
191
192# Returns TRUE if the PHY supports DFS
193phy_supports_dfs() {
194 local phy="${1}"
195 assert isset phy
196
197 local driver="$(phy_get_driver "${phy}")"
198 if ! isset driver; then
199 return ${EXIT_ERROR}
200 fi
201
202 # This is basically a whilelist of drivers which support this
203 # There is no better detection
204 case "${driver}" in
205 ath10k_*|ath9k|ath5k)
206 return ${EXIT_TRUE}
207 ;;
208 *)
209 return ${EXIT_FALSE}
210 ;;
211 esac
212}