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"
40 WIRELESS_ENVIRONMENTS
=( "indoor+outdoor" "indoor" "outdoor" )
41 WIRELESS_DEFAULT_ENVIRONMENT
="${WIRELESS_ENVIRONMENTS[0]}"
49 cli_wireless_network
"$@"
52 error
"Unrecognized argument: ${action}"
68 while [ $# -gt 0 ]; do
71 address
=$
(cli_get_val
"${1}")
74 channel
=$
(cli_get_val
"${1}")
77 phy
=$
(cli_get_val
"${1}")
81 type=$
(cli_get_val
"${1}")
84 [ "${type}" = "ap" ] && type="__ap"
87 error
"Unrecognized argument: ${1}"
95 ibss|managed|monitor|__ap
)
101 log ERROR
"Unknown type: ${type}"
107 assert phy_exists
${phy}
108 isset address || address
=$
(mac_generate
)
110 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
113 if [ ${ret} -eq ${EXIT_OK} ]; then
114 log DEBUG
"created wireless device '${device}' (${type})"
116 if isset address
; then
117 device_set_address
${device} ${address}
120 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
124 if isset channel
; then
125 wireless_set_channel
"${device}" "${channel}" "auto" ||
return $?
135 if ! device_exists
${device}; then
139 # Tear down the device (if necessary).
140 device_set_down
${device}
143 cmd_quiet iw dev
${device} del
146 if [ ${ret} -eq ${EXIT_OK} ]; then
147 log DEBUG
"removed wireless device '${device}'"
149 log ERROR
"could not remove wireless device '${device}': ${ret}"
155 wireless_get_reg_domain
() {
156 # Returns the country code for the wireless device.
157 # Defaults to 00 = world if unset.
158 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
161 wireless_init_reg_domain
() {
162 local country_code
="$(wireless_get_reg_domain)"
164 wireless_set_reg_domain
"${country_code}" --no-reset
167 wireless_set_reg_domain
() {
171 while [ $# -gt 0 ]; do
177 log ERROR
"Ignoring invalid option: ${1}"
186 # Check if configuration value is valid
187 if ! wireless_valid_reg_domain
"${country_code}"; then
188 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
192 # Before the wireless reg domain is set, it helps to reset to 00 first.
193 if enabled
reset; then
194 iw reg
set 00 &>/dev
/null
197 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
198 iw reg
set "${country_code}"
201 wireless_valid_reg_domain
() {
202 local country_code
="${1}"
204 # Empty country codes are invalid
205 isset country_code ||
return ${EXIT_FALSE}
207 local valid_country_codes
="$(wireless_list_reg_domains)"
209 if list_match
"${country_code}" ${valid_country_codes}; then
216 wireless_list_reg_domains
() {
217 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
218 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
224 # Check if line starts with "country"
225 [ "${line:0:7}" = "country" ] ||
continue
229 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
232 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
233 wireless_channel_to_frequency
() {
236 # Works only for valid channel numbers
237 if ! wireless_channel_is_valid
"${channel}"; then
238 log ERROR
"Invalid wireless channel: ${channel}"
245 print
"$(( 2407 + (${channel} * 5)))"
256 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
257 print
"$(( 5000 + (${channel} * 5)))"
265 wireless_frequency_to_channel
() {
268 assert isinteger frequency
270 # Everything that is too high
271 if [ ${frequency} -gt 5825 ]; then
275 elif [ ${frequency} -gt 5000 ]; then
276 (( frequency
= frequency
- 5000 ))
278 # Must be divisible by 5
279 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
281 print
"$(( frequency / 5 ))"
283 # 2.4 GHz Band - Channel 14
284 elif [ ${frequency} -eq 2484 ]; then
288 elif [ ${frequency} -gt 2407 ]; then
289 (( frequency
= frequency
- 2407 ))
291 # Must be divisible by 5
292 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
294 print
"$(( frequency / 5 ))"
304 wireless_channel_is_valid
() {
309 [123456789]|
1[0123]|
14)
314 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
319 # Invalid channel number given
323 wireless_channel_bandwidth_is_valid
() {
327 local bandwidth
="${2}"
328 assert isset bandwidth
330 local bandwidths
="${WIRELESS_CHANNEL_BANDWIDTHS["${mode}"]}"
332 list_match
"${bandwidth}" ${bandwidths}
335 wireless_channel_is_ht40_plus
() {
337 assert isinteger channel
340 if [ ${channel} -le 6 ]; then
347 wireless_channel_is_ht40_minus
() {
349 assert isinteger channel
352 if [ ${channel} -ge 6 ]; then
359 wireless_set_channel
() {
362 local bandwidth
="${3}"
364 # Check if the device exists
365 if ! device_exists
"${device}"; then
366 log ERROR
"No such device: ${device}"
370 # Check if the channel number is valid
371 if ! wireless_channel_is_valid
"${channel}"; then
372 log ERROR
"Invalid wireless channel: ${channel}"
377 if [ "${bandwidth}" = "auto" ]; then
378 local phy
="$(device_get_phy "${device}")"
380 # Offset of a 40 MHz channel
383 if wireless_channel_is_ht40_plus
"${channel}" \
384 && phy_supports_ht_capability
"${phy}" "HT40+" \
385 && phy_supports_channel
"${phy}" $
(( channel
+ ht_offset
)); then
388 elif wireless_channel_is_ht40_minus
"${channel}" \
389 && phy_supports_ht_capability
"${phy}" "HT40-" \
390 && phy_supports_channel
"${phy}" $
(( channel
- ht_offset
)); then
395 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
396 cmd iw dev
"${device}" set channel "${channel}" "${ht_flag}"
399 wireless_pre_shared_key_is_valid
() {
405 # For WPA*, the key must be between 8 and 63 chars
406 if [ ${l} -lt 8 ] ||
[ ${l} -gt 63 ]; then
410 # Can only contain ASCII chararcters
411 if contains_non_ascii_characters
"${psk}"; then
419 wireless_client_is_connected
() {
422 device_has_carrier
"${device}"
425 wireless_ibss_join
() {
434 while [ $# -gt 0 ]; do
437 bssid
="$(cli_get_val "${1}")"
440 essid
="$(cli_get_val "${1}")"
443 local channel
="$(cli_get_val "${1}")"
445 # Save the frequency of the channel instead
446 # of the channel itself.
447 if isset channel
; then
448 frequency
="$(wireless_channel_to_frequency ${channel})"
458 assert isinteger frequency
461 device_set_up
"${device}"
463 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
464 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
465 "${frequency}" fixed-freq
"${bssid}"
468 wireless_ibss_leave
() {
472 log INFO
"${device} leaving ibss network"
473 cmd_quiet iw dev
"${device}" ibss leave
476 wireless_is_radar_frequency
() {
477 local frequency
="${1}"
478 assert isset frequency
480 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
488 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
490 # Create an 802.11 monitoring device
491 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
496 # Bring up the device
497 device_set_up
"${monitor_device}"
500 tcpdump
-i "${monitor_device}" "$@"
502 # Remove the monitoring interface.
503 wireless_remove
"${monitor_device}"
507 log ERROR
"Could not create a monitoring interface on ${device}"
515 wireless_get_ht_caps
() {
519 local phy
="$(device_get_phy "${device}")"
521 log ERROR
"Could not determine PHY for ${device}"
525 network-phy-list-ht-caps
"${phy}"
528 wireless_get_vht_caps
() {
532 local phy
="$(device_get_phy "${device}")"
534 log ERROR
"Could not determine PHY for ${device}"
538 network-phy-list-vht-caps
"${phy}"
541 wireless_supports_acs
() {
545 local phy
="$(device_get_phy "${device}")"
547 log ERROR
"Could not determine PHY for ${device}"
551 phy_supports_acs
"${phy}"
554 wireless_supports_dfs
() {
558 local phy
="$(device_get_phy "${device}")"
560 log ERROR
"Could not determine PHY for ${device}"
564 phy_supports_dfs
"${phy}"
567 wireless_environment_is_valid
() {
568 local environment
="${1}"
570 list_match
"${environment}" "${WIRELESS_ENVIRONMENTS[@]}"