]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.phy
wireless-ap: Automatically enable all supported ciphers
[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
01648ba6
MT
24cli_device_status_phy() {
25 local phy="${1}"
26 assert phy_exists "${phy}"
27
28 local address="$(phy_get_address "${phy}")"
29 cli_print_fmt1 1 "Address" "${address}"
30
31 # Show kernel module
32 local driver="$(phy_get_driver "${phy}")"
33 if isset driver; then
34 cli_print_fmt1 1 "Driver" "${driver}"
35 fi
36
37 cli_space
38
39 local devices="$(phy_get_devices "${phy}")"
40 if isset devices; then
41 cli_headline 2 "Soft interfaces"
42
43 local device
44 for device in ${devices}; do
45 cli_print 2 "* %s" "${device}"
46 done
47 cli_space
48 fi
49
50 cli_headline 2 "Features"
51
52 cli_print_fmt1 2 "Automatic Channel Selection" \
53 "$(phy_supports_acs "${phy}" && print "Supported" || print "Not Supported")"
54 cli_print_fmt1 2 "DFS" \
55 "$(phy_supports_dfs "${phy}" && print "Supported" || print "Not Supported")"
56
57 cli_space
58
59 return ${EXIT_OK}
60}
61
1c6a4e30 62phy_dir() {
590cb657
MT
63 local phy=${1}
64
65 echo "${PHY_DIR}/${phy}"
66}
67
1c6a4e30 68phy_exists() {
590cb657
MT
69 local phy=${1}
70 assert isset phy
71
72 [ -d "$(phy_dir ${phy})" ]
73}
74
1c6a4e30 75phy_list() {
60b1f378 76 list_directory "$(phy_dir)"
590cb657
MT
77}
78
1c6a4e30 79phy_get() {
4733a336 80 local info="${1}"
590cb657
MT
81 local phy
82
4733a336
MT
83 # As this is already a valid phy, we don't need to search on...
84 if phy_exists "${info}"; then
85 print "${info}"
86 return ${EXIT_OK}
590cb657
MT
87 fi
88
4733a336
MT
89 # If this is an existing device, we can figure out the name of the
90 # phy directly.
91 if device_exists ${info}; then
92 phy="$(device_get_phy ${info})"
93
94 # If we just got the MAC address we need to walk though all
95 # available phys and find the right one.
96 elif mac_is_valid ${info}; then
590cb657
MT
97 local i
98 for i in $(phy_list); do
99 if [ "${info}" = "$(phy_get_address ${i})" ]; then
100 phy=${i}
101 break
102 fi
103 done
104 fi
105
31670741
MT
106 log DEBUG "Searching for phy = ${info}, found ${phy:-none}"
107
590cb657
MT
108 if [ -z "${phy}" ]; then
109 return ${EXIT_ERROR}
110 fi
111
112 echo "${phy}"
113 return ${EXIT_OK}
114}
115
1c6a4e30 116phy_get_address() {
590cb657
MT
117 local phy=${1}
118 assert isset phy
119
120 local path="$(phy_dir ${phy})/macaddress"
121 [ -r "${path}" ] || return ${EXIT_ERROR}
122
123 print "$(<${path})"
124}
b8026986 125
4ad5981f
MT
126phy_get_driver() {
127 local phy="${1}"
128 assert isset phy
129
130 get_driver_from_path "$(phy_dir "${phy}")/device/driver/module"
131}
132
1c6a4e30 133phy_get_devices() {
b8026986
MT
134 local phy="${1}"
135 assert isset phy
136
137 local device
91aeb1be 138 for device in $(device_list); do
b8026986
MT
139 local p="$(device_get_phy "${device}")"
140
141 if [ "${phy}" = "${p}" ]; then
142 print "${device}"
143 fi
144 done
145}
208f7452
MT
146
147phy_list_leds() {
148 local phy="${1}"
149
150 # Check if the PHY exists
151 assert phy_exists "${phy}"
152
153 local led
154 for led in $(list_directory /sys/class/leds); do
155 # Get basename of the LED
156 led=${led%*/}
157
158 if [[ ${led} =~ ${phy}(:.*)?$ ]]; then
159 print "${led}"
160 fi
161 done
162}
163
164# This function tries to automatically configure LEDs to
165# something useful
166phy_leds_autoconf() {
167 local phy="${1}"
168 assert isset phy
169
170 local led
171 for led in $(phy_list_leds "${phy}"); do
172 # Skip some types of LEDs
173 case "${led}" in
174 # Pretty much everything we tested from Ralink
175 # locked up the kernel after a couple of seconds
176 rt*)
177 continue
178 ;;
179 esac
180
181 # We try to set the LED into tpt mode (flashing on activity),
182 # but will fallback to tx mode if that isn't supported
183 local trigger
184 for trigger in "${phy}tpt" "${phy}tx"; do
185 if led_set_trigger "${led}" "${trigger}"; then
186 break
187 fi
188 done
189 done
190
191 return ${EXIT_OK}
192}
394cff53
MT
193
194phy_supports_channel() {
195 local phy="${1}"
196 assert isset phy
197
198 local channel="${2}"
199 assert isinteger channel
200
201 local _channel _frequency _dfs _max_tx_power
202 while read -r _channel _frequency _dfs _max_tx_power; do
203 if [ "${channel}" = "${_channel}" ]; then
204 return ${EXIT_TRUE}
205 fi
206 done <<< "$(network-phy-list-channels "${phy}")"
207
208 return ${EXIT_FALSE}
209}
109884be 210
2e4e3c88
MT
211phy_list_ciphers() {
212 local phy="${1}"
213 assert isset phy
214
215 network-phy-list-ciphers "${phy}"
216}
217
218phy_supports_cipher() {
219 local phy="${1}"
220 assert isset phy
221
222 local cipher="${2}"
223 assert isset cipher
224
225 list_match "${cipher}" $(phy_list_ciphers "${phy}")
226}
227
109884be
MT
228__phy_list_ht_capabilities() {
229 local phy="${1}"
230 assert isset phy
231
232 local capabilities="$(network-phy-list-ht-caps "${phy}")"
233
234 print "${capabilities//[\[\]]/ }"
235}
236
237phy_supports_ht_capability() {
238 local phy="${1}"
239 assert isset phy
240
241 local capability="${2}"
242 assert isset capability
243
244 list_match "${capability}" $(__phy_list_ht_capabilities "${phy}")
245}
dc6d97fb 246
1b4aa2ca
MT
247# Returns TRUE if the PHY supports ACS
248phy_supports_acs() {
249 local phy="${1}"
250 assert isset phy
251
252 local driver="$(phy_get_driver "${phy}")"
253 if ! isset driver; then
254 return ${EXIT_ERROR}
255 fi
256
257 # This is basically a whilelist of drivers which support this
258 # There is no better detection
259 case "${driver}" in
260 ath10k_*|ath9k|ath5k)
261 return ${EXIT_TRUE}
262 ;;
263 *)
264 return ${EXIT_FALSE}
265 ;;
266 esac
267}
268
dc6d97fb
MT
269# Returns TRUE if the PHY supports DFS
270phy_supports_dfs() {
271 local phy="${1}"
272 assert isset phy
273
274 local driver="$(phy_get_driver "${phy}")"
275 if ! isset driver; then
276 return ${EXIT_ERROR}
277 fi
278
279 # This is basically a whilelist of drivers which support this
280 # There is no better detection
281 case "${driver}" in
282 ath10k_*|ath9k|ath5k)
283 return ${EXIT_TRUE}
284 ;;
285 *)
286 return ${EXIT_FALSE}
287 ;;
288 esac
289}