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"
37 while [ $# -gt 0 ]; do
40 address
=$
(cli_get_val
${1})
43 phy
=$
(cli_get_val
${1})
47 type=$
(cli_get_val
${1})
50 [ "${type}" = "ap" ] && type="__ap"
53 error
"Unrecognized argument: ${1}"
60 assert isoneof
type ibss managed monitor __ap
61 assert phy_exists
${phy}
62 isset address || address
=$
(mac_generate
)
64 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
67 if [ ${ret} -eq ${EXIT_OK} ]; then
68 log DEBUG
"created wireless device '${device}' (${type})"
70 if isset address
; then
71 device_set_address
${device} ${address}
74 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
84 if ! device_exists
${device}; then
88 # Tear down the device (if necessary).
89 device_set_down
${device}
92 cmd_quiet iw dev
${device} del
95 if [ ${ret} -eq ${EXIT_OK} ]; then
96 log DEBUG
"removed wireless device '${device}'"
98 log ERROR
"could not remove wireless device '${device}': ${ret}"
104 wireless_get_reg_domain
() {
105 # Returns the country code for the wireless device.
106 # Defaults to 00 = world if unset.
107 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
110 wireless_init_reg_domain
() {
111 local country_code
="$(wireless_get_reg_domain)"
113 wireless_set_reg_domain
"${country_code}" --no-reset
116 wireless_set_reg_domain
() {
120 while [ $# -gt 0 ]; do
126 log ERROR
"Ignoring invalid option: ${1}"
135 # Check if configuration value is valid
136 if ! wireless_valid_reg_domain
"${country_code}"; then
137 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
141 # Before the wireless reg domain is set, it helps to reset to 00 first.
142 if enabled
reset; then
143 iw reg
set 00 &>/dev
/null
146 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
147 iw reg
set "${country_code}"
150 wireless_valid_reg_domain
() {
151 local country_code
="${1}"
153 # Empty country codes are invalid
154 isset country_code ||
return ${EXIT_FALSE}
156 local valid_country_codes
="$(wireless_list_reg_domains)"
158 if list_match
"${country_code}" ${valid_country_codes}; then
165 wireless_list_reg_domains
() {
166 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
167 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
173 # Check if line starts with "country"
174 [ "${line:0:7}" = "country" ] ||
continue
178 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
181 wireless_channel_to_frequency
() {
182 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
187 # Channel number must be positive.
188 assert
[ "${channel}" -gt 0 ]
193 print
"$(( 2407 + (${channel} * 5)))"
204 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
205 print
"$(( 5000 + (${channel} * 5)))"
213 wireless_set_channel
() {
220 device_exists
${device} ||
return ${EXIT_ERROR}
222 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
223 cmd_quiet iw dev
${device} set channel
${channel}
226 wireless_ibss_join
() {
235 while [ $# -gt 0 ]; do
238 bssid
="$(cli_get_val ${1})"
241 essid
="$(cli_get_val ${1})"
244 local channel
="$(cli_get_val ${1})"
246 # Save the frequency of the channel instead
247 # of the channel itself.
248 if isset channel
; then
249 frequency
="$(wireless_channel_to_frequency ${channel})"
259 assert isinteger frequency
262 device_set_up
"${device}"
264 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
265 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
266 "${frequency}" fixed-freq
"${bssid}"
269 wireless_ibss_leave
() {
273 log INFO
"${device} leaving ibss network"
274 cmd_quiet iw dev
"${device}" ibss leave
277 wireless_is_radar_frequency
() {
278 local frequency
="${1}"
279 assert isset frequency
281 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
289 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
291 # Create an 802.11 monitoring device
292 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
297 # Bring up the device
298 device_set_up
"${monitor_device}"
301 tcpdump
-i "${monitor_device}" "$@"
303 # Remove the monitoring interface.
304 wireless_remove
"${monitor_device}"
308 log ERROR
"Could not create a monitoring interface on ${device}"