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