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}"
64 assert isoneof
type ibss managed monitor __ap
65 assert phy_exists
${phy}
66 isset address || address
=$
(mac_generate
)
68 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
71 if [ ${ret} -eq ${EXIT_OK} ]; then
72 log DEBUG
"created wireless device '${device}' (${type})"
74 if isset address
; then
75 device_set_address
${device} ${address}
78 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
82 if isset channel
; then
83 wireless_set_channel
"${device}" "${channel}" ||
return $?
93 if ! device_exists
${device}; then
97 # Tear down the device (if necessary).
98 device_set_down
${device}
101 cmd_quiet iw dev
${device} del
104 if [ ${ret} -eq ${EXIT_OK} ]; then
105 log DEBUG
"removed wireless device '${device}'"
107 log ERROR
"could not remove wireless device '${device}': ${ret}"
113 wireless_get_reg_domain
() {
114 # Returns the country code for the wireless device.
115 # Defaults to 00 = world if unset.
116 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
119 wireless_init_reg_domain
() {
120 local country_code
="$(wireless_get_reg_domain)"
122 wireless_set_reg_domain
"${country_code}" --no-reset
125 wireless_set_reg_domain
() {
129 while [ $# -gt 0 ]; do
135 log ERROR
"Ignoring invalid option: ${1}"
144 # Check if configuration value is valid
145 if ! wireless_valid_reg_domain
"${country_code}"; then
146 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
150 # Before the wireless reg domain is set, it helps to reset to 00 first.
151 if enabled
reset; then
152 iw reg
set 00 &>/dev
/null
155 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
156 iw reg
set "${country_code}"
159 wireless_valid_reg_domain
() {
160 local country_code
="${1}"
162 # Empty country codes are invalid
163 isset country_code ||
return ${EXIT_FALSE}
165 local valid_country_codes
="$(wireless_list_reg_domains)"
167 if list_match
"${country_code}" ${valid_country_codes}; then
174 wireless_list_reg_domains
() {
175 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
176 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
182 # Check if line starts with "country"
183 [ "${line:0:7}" = "country" ] ||
continue
187 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
190 wireless_channel_to_frequency
() {
191 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
196 # Channel number must be positive.
197 assert
[ "${channel}" -gt 0 ]
202 print
"$(( 2407 + (${channel} * 5)))"
213 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
214 print
"$(( 5000 + (${channel} * 5)))"
222 wireless_frequency_to_channel
() {
225 assert isinteger frequency
227 # Everything that is too high
228 if [ ${frequency} -gt 5825 ]; then
232 elif [ ${frequency} -gt 5000 ]; then
233 (( frequency
= frequency
- 5000 ))
235 # Must be divisible by 5
236 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
238 print
"$(( frequency / 5 ))"
240 # 2.4 GHz Band - Channel 14
241 elif [ ${frequency} -eq 2484 ]; then
245 elif [ ${frequency} -gt 2407 ]; then
246 (( frequency
= frequency
- 2407 ))
248 # Must be divisible by 5
249 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
251 print
"$(( frequency / 5 ))"
261 wireless_set_channel
() {
268 device_exists
${device} ||
return ${EXIT_ERROR}
270 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
271 cmd_quiet iw dev
${device} set channel
${channel}
274 wireless_ibss_join
() {
283 while [ $# -gt 0 ]; do
286 bssid
="$(cli_get_val ${1})"
289 essid
="$(cli_get_val ${1})"
292 local channel
="$(cli_get_val ${1})"
294 # Save the frequency of the channel instead
295 # of the channel itself.
296 if isset channel
; then
297 frequency
="$(wireless_channel_to_frequency ${channel})"
307 assert isinteger frequency
310 device_set_up
"${device}"
312 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
313 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
314 "${frequency}" fixed-freq
"${bssid}"
317 wireless_ibss_leave
() {
321 log INFO
"${device} leaving ibss network"
322 cmd_quiet iw dev
"${device}" ibss leave
325 wireless_is_radar_frequency
() {
326 local frequency
="${1}"
327 assert isset frequency
329 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
337 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
339 # Create an 802.11 monitoring device
340 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
345 # Bring up the device
346 device_set_up
"${monitor_device}"
349 tcpdump
-i "${monitor_device}" "$@"
351 # Remove the monitoring interface.
352 wireless_remove
"${monitor_device}"
356 log ERROR
"Could not create a monitoring interface on ${device}"