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"
34 cli_wireless_network
"$@"
37 error
"Unrecognized argument: ${action}"
53 while [ $# -gt 0 ]; do
56 address
=$
(cli_get_val
"${1}")
59 channel
=$
(cli_get_val
"${1}")
62 phy
=$
(cli_get_val
"${1}")
66 type=$
(cli_get_val
"${1}")
69 [ "${type}" = "ap" ] && type="__ap"
72 error
"Unrecognized argument: ${1}"
80 ibss|managed|monitor|__ap
)
86 log ERROR
"Unknown type: ${type}"
92 assert phy_exists
${phy}
93 isset address || address
=$
(mac_generate
)
95 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
98 if [ ${ret} -eq ${EXIT_OK} ]; then
99 log DEBUG
"created wireless device '${device}' (${type})"
101 if isset address
; then
102 device_set_address
${device} ${address}
105 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
109 if isset channel
; then
110 wireless_set_channel
"${device}" "${channel}" ||
return $?
120 if ! device_exists
${device}; then
124 # Tear down the device (if necessary).
125 device_set_down
${device}
128 cmd_quiet iw dev
${device} del
131 if [ ${ret} -eq ${EXIT_OK} ]; then
132 log DEBUG
"removed wireless device '${device}'"
134 log ERROR
"could not remove wireless device '${device}': ${ret}"
140 wireless_get_reg_domain
() {
141 # Returns the country code for the wireless device.
142 # Defaults to 00 = world if unset.
143 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
146 wireless_init_reg_domain
() {
147 local country_code
="$(wireless_get_reg_domain)"
149 wireless_set_reg_domain
"${country_code}" --no-reset
152 wireless_set_reg_domain
() {
156 while [ $# -gt 0 ]; do
162 log ERROR
"Ignoring invalid option: ${1}"
171 # Check if configuration value is valid
172 if ! wireless_valid_reg_domain
"${country_code}"; then
173 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
177 # Before the wireless reg domain is set, it helps to reset to 00 first.
178 if enabled
reset; then
179 iw reg
set 00 &>/dev
/null
182 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
183 iw reg
set "${country_code}"
186 wireless_valid_reg_domain
() {
187 local country_code
="${1}"
189 # Empty country codes are invalid
190 isset country_code ||
return ${EXIT_FALSE}
192 local valid_country_codes
="$(wireless_list_reg_domains)"
194 if list_match
"${country_code}" ${valid_country_codes}; then
201 wireless_list_reg_domains
() {
202 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
203 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
209 # Check if line starts with "country"
210 [ "${line:0:7}" = "country" ] ||
continue
214 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
217 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
218 wireless_channel_to_frequency
() {
221 # Works only for valid channel numbers
222 if ! wireless_channel_is_valid
"${channel}"; then
223 log ERROR
"Invalid wireless channel: ${channel}"
230 print
"$(( 2407 + (${channel} * 5)))"
241 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
242 print
"$(( 5000 + (${channel} * 5)))"
250 wireless_frequency_to_channel
() {
253 assert isinteger frequency
255 # Everything that is too high
256 if [ ${frequency} -gt 5825 ]; then
260 elif [ ${frequency} -gt 5000 ]; then
261 (( frequency
= frequency
- 5000 ))
263 # Must be divisible by 5
264 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
266 print
"$(( frequency / 5 ))"
268 # 2.4 GHz Band - Channel 14
269 elif [ ${frequency} -eq 2484 ]; then
273 elif [ ${frequency} -gt 2407 ]; then
274 (( frequency
= frequency
- 2407 ))
276 # Must be divisible by 5
277 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
279 print
"$(( frequency / 5 ))"
289 wireless_channel_is_valid
() {
294 [123456789]|
1[0123]|
14)
299 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
304 # Invalid channel number given
308 wireless_set_channel
() {
312 # Check if the device exists
313 if ! device_exists
"${device}"; then
314 log ERROR
"No such device: ${device}"
318 # Check if the channel number is valid
319 if ! wireless_channel_is_valid
"${channel}"; then
320 log ERROR
"Invalid wireless channel: ${channel}"
324 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
325 cmd iw dev
"${device}" set channel
"${channel}"
328 wireless_ibss_join
() {
337 while [ $# -gt 0 ]; do
340 bssid
="$(cli_get_val "${1}")"
343 essid
="$(cli_get_val "${1}")"
346 local channel
="$(cli_get_val "${1}")"
348 # Save the frequency of the channel instead
349 # of the channel itself.
350 if isset channel
; then
351 frequency
="$(wireless_channel_to_frequency ${channel})"
361 assert isinteger frequency
364 device_set_up
"${device}"
366 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
367 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
368 "${frequency}" fixed-freq
"${bssid}"
371 wireless_ibss_leave
() {
375 log INFO
"${device} leaving ibss network"
376 cmd_quiet iw dev
"${device}" ibss leave
379 wireless_is_radar_frequency
() {
380 local frequency
="${1}"
381 assert isset frequency
383 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
391 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
393 # Create an 802.11 monitoring device
394 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
399 # Bring up the device
400 device_set_up
"${monitor_device}"
403 tcpdump
-i "${monitor_device}" "$@"
405 # Remove the monitoring interface.
406 wireless_remove
"${monitor_device}"
410 log ERROR
"Could not create a monitoring interface on ${device}"