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"
38 while [ $# -gt 0 ]; do
41 address
=$
(cli_get_val
${1})
44 channel
=$
(cli_get_val
"${1}")
47 phy
=$
(cli_get_val
${1})
51 type=$
(cli_get_val
${1})
54 [ "${type}" = "ap" ] && type="__ap"
57 error
"Unrecognized argument: ${1}"
65 ibss|managed|monitor|__ap
)
71 log ERROR
"Unknown type: ${type}"
77 assert phy_exists
${phy}
78 isset address || address
=$
(mac_generate
)
80 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
83 if [ ${ret} -eq ${EXIT_OK} ]; then
84 log DEBUG
"created wireless device '${device}' (${type})"
86 if isset address
; then
87 device_set_address
${device} ${address}
90 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
94 if isset channel
; then
95 wireless_set_channel
"${device}" "${channel}" ||
return $?
105 if ! device_exists
${device}; then
109 # Tear down the device (if necessary).
110 device_set_down
${device}
113 cmd_quiet iw dev
${device} del
116 if [ ${ret} -eq ${EXIT_OK} ]; then
117 log DEBUG
"removed wireless device '${device}'"
119 log ERROR
"could not remove wireless device '${device}': ${ret}"
125 wireless_get_reg_domain
() {
126 # Returns the country code for the wireless device.
127 # Defaults to 00 = world if unset.
128 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
131 wireless_init_reg_domain
() {
132 local country_code
="$(wireless_get_reg_domain)"
134 wireless_set_reg_domain
"${country_code}" --no-reset
137 wireless_set_reg_domain
() {
141 while [ $# -gt 0 ]; do
147 log ERROR
"Ignoring invalid option: ${1}"
156 # Check if configuration value is valid
157 if ! wireless_valid_reg_domain
"${country_code}"; then
158 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
162 # Before the wireless reg domain is set, it helps to reset to 00 first.
163 if enabled
reset; then
164 iw reg
set 00 &>/dev
/null
167 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
168 iw reg
set "${country_code}"
171 wireless_valid_reg_domain
() {
172 local country_code
="${1}"
174 # Empty country codes are invalid
175 isset country_code ||
return ${EXIT_FALSE}
177 local valid_country_codes
="$(wireless_list_reg_domains)"
179 if list_match
"${country_code}" ${valid_country_codes}; then
186 wireless_list_reg_domains
() {
187 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
188 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
194 # Check if line starts with "country"
195 [ "${line:0:7}" = "country" ] ||
continue
199 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
202 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
203 wireless_channel_to_frequency
() {
206 # Works only for valid channel numbers
207 if ! wireless_channel_is_valid
"${channel}"; then
208 log ERROR
"Invalid wireless channel: ${channel}"
215 print
"$(( 2407 + (${channel} * 5)))"
226 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
227 print
"$(( 5000 + (${channel} * 5)))"
235 wireless_frequency_to_channel
() {
238 assert isinteger frequency
240 # Everything that is too high
241 if [ ${frequency} -gt 5825 ]; then
245 elif [ ${frequency} -gt 5000 ]; then
246 (( frequency
= frequency
- 5000 ))
248 # Must be divisible by 5
249 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
251 print
"$(( frequency / 5 ))"
253 # 2.4 GHz Band - Channel 14
254 elif [ ${frequency} -eq 2484 ]; then
258 elif [ ${frequency} -gt 2407 ]; then
259 (( frequency
= frequency
- 2407 ))
261 # Must be divisible by 5
262 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
264 print
"$(( frequency / 5 ))"
274 wireless_channel_is_valid
() {
279 [123456789]|
1[0123]|
14)
284 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
289 # Invalid channel number given
293 wireless_set_channel
() {
297 # Check if the device exists
298 if ! device_exists
"${device}"; then
299 log ERROR
"No such device: ${device}"
303 # Check if the channel number is valid
304 if ! wireless_channel_is_valid
"${channel}"; then
305 log ERROR
"Invalid wireless channel: ${channel}"
309 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
310 cmd iw dev
"${device}" set channel
"${channel}"
313 wireless_ibss_join
() {
322 while [ $# -gt 0 ]; do
325 bssid
="$(cli_get_val ${1})"
328 essid
="$(cli_get_val ${1})"
331 local channel
="$(cli_get_val ${1})"
333 # Save the frequency of the channel instead
334 # of the channel itself.
335 if isset channel
; then
336 frequency
="$(wireless_channel_to_frequency ${channel})"
346 assert isinteger frequency
349 device_set_up
"${device}"
351 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
352 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
353 "${frequency}" fixed-freq
"${bssid}"
356 wireless_ibss_leave
() {
360 log INFO
"${device} leaving ibss network"
361 cmd_quiet iw dev
"${device}" ibss leave
364 wireless_is_radar_frequency
() {
365 local frequency
="${1}"
366 assert isset frequency
368 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
376 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
378 # Create an 802.11 monitoring device
379 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
384 # Bring up the device
385 device_set_up
"${monitor_device}"
388 tcpdump
-i "${monitor_device}" "$@"
390 # Remove the monitoring interface.
391 wireless_remove
"${monitor_device}"
395 log ERROR
"Could not create a monitoring interface on ${device}"