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 WEP NONE"
38 cli_wireless_network
"$@"
41 error
"Unrecognized argument: ${action}"
57 while [ $# -gt 0 ]; do
60 address
=$
(cli_get_val
"${1}")
63 channel
=$
(cli_get_val
"${1}")
66 phy
=$
(cli_get_val
"${1}")
70 type=$
(cli_get_val
"${1}")
73 [ "${type}" = "ap" ] && type="__ap"
76 error
"Unrecognized argument: ${1}"
84 ibss|managed|monitor|__ap
)
90 log ERROR
"Unknown type: ${type}"
96 assert phy_exists
${phy}
97 isset address || address
=$
(mac_generate
)
99 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
102 if [ ${ret} -eq ${EXIT_OK} ]; then
103 log DEBUG
"created wireless device '${device}' (${type})"
105 if isset address
; then
106 device_set_address
${device} ${address}
109 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
113 if isset channel
; then
114 wireless_set_channel
"${device}" "${channel}" ||
return $?
124 if ! device_exists
${device}; then
128 # Tear down the device (if necessary).
129 device_set_down
${device}
132 cmd_quiet iw dev
${device} del
135 if [ ${ret} -eq ${EXIT_OK} ]; then
136 log DEBUG
"removed wireless device '${device}'"
138 log ERROR
"could not remove wireless device '${device}': ${ret}"
144 wireless_get_reg_domain
() {
145 # Returns the country code for the wireless device.
146 # Defaults to 00 = world if unset.
147 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
150 wireless_init_reg_domain
() {
151 local country_code
="$(wireless_get_reg_domain)"
153 wireless_set_reg_domain
"${country_code}" --no-reset
156 wireless_set_reg_domain
() {
160 while [ $# -gt 0 ]; do
166 log ERROR
"Ignoring invalid option: ${1}"
175 # Check if configuration value is valid
176 if ! wireless_valid_reg_domain
"${country_code}"; then
177 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
181 # Before the wireless reg domain is set, it helps to reset to 00 first.
182 if enabled
reset; then
183 iw reg
set 00 &>/dev
/null
186 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
187 iw reg
set "${country_code}"
190 wireless_valid_reg_domain
() {
191 local country_code
="${1}"
193 # Empty country codes are invalid
194 isset country_code ||
return ${EXIT_FALSE}
196 local valid_country_codes
="$(wireless_list_reg_domains)"
198 if list_match
"${country_code}" ${valid_country_codes}; then
205 wireless_list_reg_domains
() {
206 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
207 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
213 # Check if line starts with "country"
214 [ "${line:0:7}" = "country" ] ||
continue
218 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
221 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
222 wireless_channel_to_frequency
() {
225 # Works only for valid channel numbers
226 if ! wireless_channel_is_valid
"${channel}"; then
227 log ERROR
"Invalid wireless channel: ${channel}"
234 print
"$(( 2407 + (${channel} * 5)))"
245 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
246 print
"$(( 5000 + (${channel} * 5)))"
254 wireless_frequency_to_channel
() {
257 assert isinteger frequency
259 # Everything that is too high
260 if [ ${frequency} -gt 5825 ]; then
264 elif [ ${frequency} -gt 5000 ]; then
265 (( frequency
= frequency
- 5000 ))
267 # Must be divisible by 5
268 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
270 print
"$(( frequency / 5 ))"
272 # 2.4 GHz Band - Channel 14
273 elif [ ${frequency} -eq 2484 ]; then
277 elif [ ${frequency} -gt 2407 ]; then
278 (( frequency
= frequency
- 2407 ))
280 # Must be divisible by 5
281 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
283 print
"$(( frequency / 5 ))"
293 wireless_channel_is_valid
() {
298 [123456789]|
1[0123]|
14)
303 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
308 # Invalid channel number given
312 wireless_set_channel
() {
316 # Check if the device exists
317 if ! device_exists
"${device}"; then
318 log ERROR
"No such device: ${device}"
322 # Check if the channel number is valid
323 if ! wireless_channel_is_valid
"${channel}"; then
324 log ERROR
"Invalid wireless channel: ${channel}"
328 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
329 cmd iw dev
"${device}" set channel
"${channel}"
332 wireless_pre_shared_key_is_valid
() {
333 local encryption_mode
="${1}"
339 case "${encryption_mode}" in
340 # For WPA*, the key must be between 8 and 63 chars
341 WPA2-PSK|WPA2-PSK-SHA256|WPA-PSK|WPA-PSK-SHA256
)
342 if [ ${l} -ge 8 ] && [ ${l} -le 63 ]; then
350 # XXX need to check if the key is entered in
351 # hex or ascii and then count the bytes
358 wireless_ibss_join
() {
367 while [ $# -gt 0 ]; do
370 bssid
="$(cli_get_val "${1}")"
373 essid
="$(cli_get_val "${1}")"
376 local channel
="$(cli_get_val "${1}")"
378 # Save the frequency of the channel instead
379 # of the channel itself.
380 if isset channel
; then
381 frequency
="$(wireless_channel_to_frequency ${channel})"
391 assert isinteger frequency
394 device_set_up
"${device}"
396 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
397 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
398 "${frequency}" fixed-freq
"${bssid}"
401 wireless_ibss_leave
() {
405 log INFO
"${device} leaving ibss network"
406 cmd_quiet iw dev
"${device}" ibss leave
409 wireless_is_radar_frequency
() {
410 local frequency
="${1}"
411 assert isset frequency
413 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
421 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
423 # Create an 802.11 monitoring device
424 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
429 # Bring up the device
430 device_set_up
"${monitor_device}"
433 tcpdump
-i "${monitor_device}" "$@"
435 # Remove the monitoring interface.
436 wireless_remove
"${monitor_device}"
440 log ERROR
"Could not create a monitoring interface on ${device}"