2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
22 # Sets the global wireless country code. Default is 00 = world.
23 WIRELESS_REGULATORY_DOMAIN
="00"
24 NETWORK_SETTINGS_FILE_PARAMS
="${NETWORK_SETTINGS_FILE_PARAMS} WIRELESS_REGULATORY_DOMAIN"
26 WIRELESS_REGULATORY_DOMAIN_DATABASE
="/usr/lib/crda/regulatory.bin"
28 WIRELESS_DEFAULT_ENCRYPTION_MODE
="NONE"
29 WIRELESS_VALID_ENCRYPTION_MODES
="WPA2-PSK-SHA256 WPA2-PSK \
30 WPA-PSK-SHA256 WPA-PSK NONE"
32 declare -A WIRELESS_CHANNEL_BANDWIDTHS
=(
33 ["802.11ac"]="20 40 80 160 80+80"
46 cli_wireless_network
"$@"
49 error
"Unrecognized argument: ${action}"
65 while [ $# -gt 0 ]; do
68 address
=$
(cli_get_val
"${1}")
71 channel
=$
(cli_get_val
"${1}")
74 phy
=$
(cli_get_val
"${1}")
78 type=$
(cli_get_val
"${1}")
81 [ "${type}" = "ap" ] && type="__ap"
84 error
"Unrecognized argument: ${1}"
92 ibss|managed|monitor|__ap
)
98 log ERROR
"Unknown type: ${type}"
104 assert phy_exists
${phy}
105 isset address || address
=$
(mac_generate
)
107 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
110 if [ ${ret} -eq ${EXIT_OK} ]; then
111 log DEBUG
"created wireless device '${device}' (${type})"
113 if isset address
; then
114 device_set_address
${device} ${address}
117 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
121 if isset channel
; then
122 wireless_set_channel
"${device}" "${channel}" "auto" ||
return $?
132 if ! device_exists
${device}; then
136 # Tear down the device (if necessary).
137 device_set_down
${device}
140 cmd_quiet iw dev
${device} del
143 if [ ${ret} -eq ${EXIT_OK} ]; then
144 log DEBUG
"removed wireless device '${device}'"
146 log ERROR
"could not remove wireless device '${device}': ${ret}"
152 wireless_get_reg_domain
() {
153 # Returns the country code for the wireless device.
154 # Defaults to 00 = world if unset.
155 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
158 wireless_init_reg_domain
() {
159 local country_code
="$(wireless_get_reg_domain)"
161 wireless_set_reg_domain
"${country_code}" --no-reset
164 wireless_set_reg_domain
() {
168 while [ $# -gt 0 ]; do
174 log ERROR
"Ignoring invalid option: ${1}"
183 # Check if configuration value is valid
184 if ! wireless_valid_reg_domain
"${country_code}"; then
185 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
189 # Before the wireless reg domain is set, it helps to reset to 00 first.
190 if enabled
reset; then
191 iw reg
set 00 &>/dev
/null
194 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
195 iw reg
set "${country_code}"
198 wireless_valid_reg_domain
() {
199 local country_code
="${1}"
201 # Empty country codes are invalid
202 isset country_code ||
return ${EXIT_FALSE}
204 local valid_country_codes
="$(wireless_list_reg_domains)"
206 if list_match
"${country_code}" ${valid_country_codes}; then
213 wireless_list_reg_domains
() {
214 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
215 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
221 # Check if line starts with "country"
222 [ "${line:0:7}" = "country" ] ||
continue
226 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
229 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
230 wireless_channel_to_frequency
() {
233 # Works only for valid channel numbers
234 if ! wireless_channel_is_valid
"${channel}"; then
235 log ERROR
"Invalid wireless channel: ${channel}"
242 print
"$(( 2407 + (${channel} * 5)))"
253 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
254 print
"$(( 5000 + (${channel} * 5)))"
262 wireless_frequency_to_channel
() {
265 assert isinteger frequency
267 # Everything that is too high
268 if [ ${frequency} -gt 5825 ]; then
272 elif [ ${frequency} -gt 5000 ]; then
273 (( frequency
= frequency
- 5000 ))
275 # Must be divisible by 5
276 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
278 print
"$(( frequency / 5 ))"
280 # 2.4 GHz Band - Channel 14
281 elif [ ${frequency} -eq 2484 ]; then
285 elif [ ${frequency} -gt 2407 ]; then
286 (( frequency
= frequency
- 2407 ))
288 # Must be divisible by 5
289 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
291 print
"$(( frequency / 5 ))"
301 wireless_channel_is_valid
() {
306 [123456789]|
1[0123]|
14)
311 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
316 # Invalid channel number given
320 wireless_channel_bandwidth_is_valid
() {
324 local bandwidth
="${2}"
325 assert isset bandwidth
327 local bandwidths
="${WIRELESS_CHANNEL_BANDWIDTHS["${mode}"]}"
329 list_match
"${bandwidth}" ${bandwidths}
332 wireless_channel_is_ht40_plus
() {
334 assert isinteger channel
337 if [ ${channel} -le 6 ]; then
344 wireless_channel_is_ht40_minus
() {
346 assert isinteger channel
349 if [ ${channel} -ge 6 ]; then
356 wireless_set_channel
() {
359 local bandwidth
="${3}"
361 # Check if the device exists
362 if ! device_exists
"${device}"; then
363 log ERROR
"No such device: ${device}"
367 # Check if the channel number is valid
368 if ! wireless_channel_is_valid
"${channel}"; then
369 log ERROR
"Invalid wireless channel: ${channel}"
374 if [ "${bandwidth}" = "auto" ]; then
375 local phy
="$(device_get_phy "${device}")"
377 # Offset of a 40 MHz channel
380 if wireless_channel_is_ht40_plus
"${channel}" \
381 && phy_supports_ht_capability
"${phy}" "HT40+" \
382 && phy_supports_channel
"${phy}" $
(( channel
+ ht_offset
)); then
385 elif wireless_channel_is_ht40_minus
"${channel}" \
386 && phy_supports_ht_capability
"${phy}" "HT40-" \
387 && phy_supports_channel
"${phy}" $
(( channel
- ht_offset
)); then
392 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
393 cmd iw dev
"${device}" set channel "${channel}" "${ht_flag}"
396 wireless_pre_shared_key_is_valid
() {
397 local encryption_mode
="${1}"
403 case "${encryption_mode}" in
404 # For WPA*, the key must be between 8 and 63 chars
405 WPA2-PSK|WPA2-PSK-SHA256|WPA-PSK|WPA-PSK-SHA256
)
406 if [ ${l} -ge 8 ] && [ ${l} -le 63 ]; then
417 wireless_client_is_connected
() {
420 device_has_carrier
"${device}"
423 wireless_ibss_join
() {
432 while [ $# -gt 0 ]; do
435 bssid
="$(cli_get_val "${1}")"
438 essid
="$(cli_get_val "${1}")"
441 local channel
="$(cli_get_val "${1}")"
443 # Save the frequency of the channel instead
444 # of the channel itself.
445 if isset channel
; then
446 frequency
="$(wireless_channel_to_frequency ${channel})"
456 assert isinteger frequency
459 device_set_up
"${device}"
461 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
462 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
463 "${frequency}" fixed-freq
"${bssid}"
466 wireless_ibss_leave
() {
470 log INFO
"${device} leaving ibss network"
471 cmd_quiet iw dev
"${device}" ibss leave
474 wireless_is_radar_frequency
() {
475 local frequency
="${1}"
476 assert isset frequency
478 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
486 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
488 # Create an 802.11 monitoring device
489 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
494 # Bring up the device
495 device_set_up
"${monitor_device}"
498 tcpdump
-i "${monitor_device}" "$@"
500 # Remove the monitoring interface.
501 wireless_remove
"${monitor_device}"
505 log ERROR
"Could not create a monitoring interface on ${device}"
513 wireless_get_ht_caps
() {
517 local phy
="$(device_get_phy "${device}")"
519 log ERROR
"Could not determine PHY for ${device}"
523 network-phy-list-ht-caps
"${phy}"
526 wireless_get_vht_caps
() {
530 local phy
="$(device_get_phy "${device}")"
532 log ERROR
"Could not determine PHY for ${device}"
536 network-phy-list-vht-caps
"${phy}"
539 wireless_supports_dfs
() {
543 local phy
="$(device_get_phy "${device}")"
545 log ERROR
"Could not determine PHY for ${device}"
549 phy_supports_dfs
"${phy}"