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