]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.wireless
wireless: Show channel number as well as frequency
[people/stevee/network.git] / src / functions / functions.wireless
CommitLineData
d76f5107 1#!/bin/bash
1578dae9
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
0e035311 5# Copyright (C) 2012 IPFire Network Development Team #
1578dae9
MT
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
d76f5107 21
31670741
MT
22# Sets the global wireless country code. Default is 00 = world.
23WIRELESS_REGULATORY_DOMAIN="00"
e9df08ad 24NETWORK_SETTINGS_FILE_PARAMS="${NETWORK_SETTINGS_FILE_PARAMS} WIRELESS_REGULATORY_DOMAIN"
31670741 25
ab9e0fd0
MT
26WIRELESS_REGULATORY_DOMAIN_DATABASE="/usr/lib/crda/regulatory.bin"
27
1c6a4e30 28wireless_create() {
d76f5107 29 local device=${1}
d76f5107 30 assert isset device
22a61046 31 shift
d76f5107 32
22a61046
MT
33 local address
34 local phy
35 local type="managed"
36
37 while [ $# -gt 0 ]; do
38 case "${1}" in
39 --address=*)
40 address=$(cli_get_val ${1})
41 ;;
42 --phy=*)
43 phy=$(cli_get_val ${1})
44 phy=$(phy_get ${phy})
45 ;;
46 --type=*)
47 type=$(cli_get_val ${1})
48
49 # ap --> __ap
50 [ "${type}" = "ap" ] && type="__ap"
51 ;;
52 *)
53 error "Unrecognized argument: ${1}"
54 return ${EXIT_ERROR}
55 ;;
56 esac
57 shift
58 done
d76f5107 59
5a38ea84 60 assert isoneof type ibss managed monitor __ap
22a61046
MT
61 assert phy_exists ${phy}
62 isset address || address=$(mac_generate)
63
64 cmd_quiet iw phy ${phy} interface add ${device} type ${type}
65 local ret=$?
d76f5107 66
22a61046
MT
67 if [ ${ret} -eq ${EXIT_OK} ]; then
68 log DEBUG "created wireless device '${device}' (${type})"
d76f5107 69
22a61046
MT
70 if isset address; then
71 device_set_address ${device} ${address}
72 fi
73 else
74 log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
d76f5107
MT
75 fi
76
22a61046 77 return ${ret}
d76f5107
MT
78}
79
1c6a4e30 80wireless_remove() {
d76f5107 81 local device=${1}
22a61046 82 assert isset device
d76f5107 83
22a61046
MT
84 if ! device_exists ${device}; then
85 return ${EXIT_OK}
86 fi
d76f5107 87
22a61046 88 # Tear down the device (if necessary).
d76f5107
MT
89 device_set_down ${device}
90
22a61046
MT
91 # Remove it.
92 cmd_quiet iw dev ${device} del
93 local ret=$?
94
95 if [ ${ret} -eq ${EXIT_OK} ]; then
96 log DEBUG "removed wireless device '${device}'"
97 else
98 log ERROR "could not remove wireless device '${device}': ${ret}"
99 fi
100
101 return ${ret}
d76f5107
MT
102}
103
1c6a4e30 104wireless_get_reg_domain() {
31670741
MT
105 # Returns the country code for the wireless device.
106 # Defaults to 00 = world if unset.
107 print "${WIRELESS_REGULATORY_DOMAIN:-00}"
108}
109
1c6a4e30 110wireless_init_reg_domain() {
31670741
MT
111 local country_code="$(wireless_get_reg_domain)"
112
06a6f01e 113 wireless_set_reg_domain "${country_code}" --no-reset
31670741
MT
114}
115
1c6a4e30 116wireless_set_reg_domain() {
06a6f01e
MT
117 local country_code
118 local reset="true"
119
120 while [ $# -gt 0 ]; do
121 case "${1}" in
122 --no-reset)
123 reset="false"
124 ;;
125 -*)
126 log ERROR "Ignoring invalid option: ${1}"
127 ;;
128 *)
129 country_code="${1}"
130 ;;
131 esac
132 shift
133 done
134
ab9e0fd0
MT
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}"
138 return ${EXIT_ERROR}
139 fi
31670741
MT
140
141 # Before the wireless reg domain is set, it helps to reset to 00 first.
06a6f01e
MT
142 if enabled reset; then
143 iw reg set 00 &>/dev/null
144 fi
31670741
MT
145
146 log INFO "Setting wireless regulatory domain country to '${country_code}'"
147 iw reg set "${country_code}"
148}
149
ab9e0fd0
MT
150wireless_valid_reg_domain() {
151 local country_code="${1}"
152
153 # Empty country codes are invalid
154 isset country_code || return ${EXIT_FALSE}
155
156 local valid_country_codes="$(wireless_list_reg_domains)"
157
158 if list_match "${country_code}" ${valid_country_codes}; then
159 return ${EXIT_TRUE}
160 fi
161
162 return ${EXIT_FALSE}
163}
164
165wireless_list_reg_domains() {
166 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
167 log ERROR "Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
168 return ${EXIT_ERROR}
169 fi
170
171 local line
172 while read line; do
173 # Check if line starts with "country"
174 [ "${line:0:7}" = "country" ] || continue
175
176 # Print country code
177 print "${line:8:2}"
178 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
179}
180
1c6a4e30 181wireless_channel_to_frequency() {
91987cc5
MT
182 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
183
184 local channel=${1}
185 assert isset channel
186
187 # Channel number must be positive.
188 assert [ "${channel}" -gt 0 ]
189
190 # 2.4 GHz band
191 case "${channel}" in
192 [123456789]|1[0123])
193 print "$(( 2407 + (${channel} * 5)))"
194 return ${EXIT_OK}
195 ;;
196 14)
197 print "2484"
198 return ${EXIT_OK}
199 ;;
200 esac
201
202 # 5 GHz band
203 case "${channel}" in
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)))"
206 return ${EXIT_OK}
207 ;;
208 esac
209
210 return ${EXIT_ERROR}
211}
212
97877f23
MT
213wireless_frequency_to_channel() {
214 local frequency=${1}
215
216 assert isinteger frequency
217
218 # Everything that is too high
219 if [ ${frequency} -gt 5825 ]; then
220 return ${EXIT_ERROR}
221
222 # 5 GHz Band
223 elif [ ${frequency} -gt 5000 ]; then
224 (( frequency = frequency - 5000 ))
225
226 # Must be divisible by 5
227 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
228
229 print "$(( frequency / 5 ))"
230
231 # 2.4 GHz Band - Channel 14
232 elif [ ${frequency} -eq 2484 ]; then
233 print "14"
234
235 # 2.4 GHz Band
236 elif [ ${frequency} -gt 2407 ]; then
237 (( frequency = frequency - 2407 ))
238
239 # Must be divisible by 5
240 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
241
242 print "$(( frequency / 5 ))"
243
244 # Everything else
245 else
246 return ${EXIT_ERROR}
247 fi
248
249 return ${EXIT_OK}
250}
251
1c6a4e30 252wireless_set_channel() {
d76f5107 253 local device=${1}
d76f5107 254 assert isset device
22a61046
MT
255
256 local channel=${2}
d76f5107
MT
257 assert isset channel
258
22a61046
MT
259 device_exists ${device} || return ${EXIT_ERROR}
260
5a38ea84 261 log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'"
22a61046 262 cmd_quiet iw dev ${device} set channel ${channel}
d76f5107 263}
91987cc5 264
1c6a4e30 265wireless_ibss_join() {
91987cc5
MT
266 local device=${1}
267 assert isset device
268 shift
269
270 local bssid
271 local essid
272 local frequency
273
274 while [ $# -gt 0 ]; do
275 case "${1}" in
276 --bssid=*)
277 bssid="$(cli_get_val ${1})"
278 ;;
279 --essid=*)
280 essid="$(cli_get_val ${1})"
281 ;;
282 --channel=*)
283 local channel="$(cli_get_val ${1})"
284
285 # Save the frequency of the channel instead
286 # of the channel itself.
287 if isset channel; then
288 frequency="$(wireless_channel_to_frequency ${channel})"
289 fi
290 ;;
291 esac
292 shift
293 done
294
295 # Check input.
296 assert ismac bssid
297 assert isset essid
298 assert isinteger frequency
299
300 # Set device up.
301 device_set_up "${device}"
302
303 log INFO "${device} joining ibss network: ${essid} (${bssid})"
304 cmd_quiet iw dev "${device}" ibss join "${essid}" \
305 "${frequency}" fixed-freq "${bssid}"
306}
307
1c6a4e30 308wireless_ibss_leave() {
91987cc5
MT
309 local device=${1}
310 assert isset device
311
312 log INFO "${device} leaving ibss network"
313 cmd_quiet iw dev "${device}" ibss leave
314}
646ae5b2 315
1c6a4e30 316wireless_is_radar_frequency() {
646ae5b2
MT
317 local frequency="${1}"
318 assert isset frequency
319
320 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
321}
5a38ea84 322
1c6a4e30 323wireless_monitor() {
5a38ea84
MT
324 local device="${1}"
325 assert isset device
326 shift
327
a23fdc0e 328 local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
5a38ea84
MT
329
330 # Create an 802.11 monitoring device
331 wireless_create "${monitor_device}" --phy="${device}" --type="monitor"
332 local ret=$?
333
334 case "${ret}" in
335 0)
336 # Bring up the device
337 device_set_up "${monitor_device}"
338
339 # Starting tcpdump
340 tcpdump -i "${monitor_device}" "$@"
341
342 # Remove the monitoring interface.
343 wireless_remove "${monitor_device}"
344 ;;
345
346 *)
347 log ERROR "Could not create a monitoring interface on ${device}"
348 return ${EXIT_ERROR}
349 ;;
350 esac
351
352 return ${EXIT_OK}
353}