]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.wireless
12204c07879b6794c99f94e9d13b0442674cb4ce
[people/ms/network.git] / src / functions / functions.wireless
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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 ###############################################################################
21
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"
25
26 WIRELESS_REGULATORY_DOMAIN_DATABASE="/usr/lib/crda/regulatory.bin"
27
28 WIRELESS_DEFAULT_ENCRYPTION_MODE="NONE"
29 WIRELESS_VALID_ENCRYPTION_MODES="WPA2-PSK-SHA256 WPA2-PSK \
30 WPA-PSK-SHA256 WPA-PSK NONE"
31
32 declare -A WIRELESS_CHANNEL_BANDWIDTHS=(
33 ["802.11ac"]="20 40 80 160 80+80"
34 ["802.11a/n"]="20 40"
35 ["802.11a"]="20 40"
36 ["802.11g/n"]="20 40"
37 ["802.11g"]="20 40"
38 )
39
40 WIRELESS_ENVIRONMENTS=( "indoor+outdoor" "indoor" "outdoor" )
41 WIRELESS_DEFAULT_ENVIRONMENT="${WIRELESS_ENVIRONMENTS[0]}"
42
43 cli_wireless() {
44 local action=${1}
45 shift 1
46
47 case "${action}" in
48 network)
49 cli_wireless_network "$@"
50 ;;
51 *)
52 error "Unrecognized argument: ${action}"
53 exit ${EXIT_ERROR}
54 ;;
55 esac
56 }
57
58 wireless_create() {
59 local device=${1}
60 assert isset device
61 shift
62
63 local address
64 local channel
65 local phy
66 local type="managed"
67
68 while [ $# -gt 0 ]; do
69 case "${1}" in
70 --address=*)
71 address=$(cli_get_val "${1}")
72 ;;
73 --channel=*)
74 channel=$(cli_get_val "${1}")
75 ;;
76 --phy=*)
77 phy=$(cli_get_val "${1}")
78 phy=$(phy_get ${phy})
79 ;;
80 --type=*)
81 type=$(cli_get_val "${1}")
82
83 # ap --> __ap
84 [ "${type}" = "ap" ] && type="__ap"
85 ;;
86 *)
87 error "Unrecognized argument: ${1}"
88 return ${EXIT_ERROR}
89 ;;
90 esac
91 shift
92 done
93
94 case "${type}" in
95 ibss|managed|monitor|__ap)
96 ;;
97 mesh-point)
98 type="mp"
99 ;;
100 *)
101 log ERROR "Unknown type: ${type}"
102 return ${EXIT_ERROR}
103 ;;
104
105 esac
106
107 assert phy_exists ${phy}
108 isset address || address=$(mac_generate)
109
110 cmd_quiet iw phy ${phy} interface add ${device} type ${type}
111 local ret=$?
112
113 if [ ${ret} -eq ${EXIT_OK} ]; then
114 log DEBUG "created wireless device '${device}' (${type})"
115
116 if isset address; then
117 device_set_address ${device} ${address}
118 fi
119 else
120 log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
121 fi
122
123 # Set the channel
124 if isset channel; then
125 wireless_set_channel "${device}" "${channel}" "auto" || return $?
126 fi
127
128 return ${ret}
129 }
130
131 wireless_remove() {
132 local device=${1}
133 assert isset device
134
135 if ! device_exists ${device}; then
136 return ${EXIT_OK}
137 fi
138
139 # Tear down the device (if necessary).
140 device_set_down ${device}
141
142 # Remove it.
143 cmd_quiet iw dev ${device} del
144 local ret=$?
145
146 if [ ${ret} -eq ${EXIT_OK} ]; then
147 log DEBUG "removed wireless device '${device}'"
148 else
149 log ERROR "could not remove wireless device '${device}': ${ret}"
150 fi
151
152 return ${ret}
153 }
154
155 wireless_get_reg_domain() {
156 # Returns the country code for the wireless device.
157 # Defaults to 00 = world if unset.
158 print "${WIRELESS_REGULATORY_DOMAIN:-00}"
159 }
160
161 wireless_init_reg_domain() {
162 local country_code="$(wireless_get_reg_domain)"
163
164 wireless_set_reg_domain "${country_code}" --no-reset
165 }
166
167 wireless_set_reg_domain() {
168 local country_code
169 local reset="true"
170
171 while [ $# -gt 0 ]; do
172 case "${1}" in
173 --no-reset)
174 reset="false"
175 ;;
176 -*)
177 log ERROR "Ignoring invalid option: ${1}"
178 ;;
179 *)
180 country_code="${1}"
181 ;;
182 esac
183 shift
184 done
185
186 # Check if configuration value is valid
187 if ! wireless_valid_reg_domain "${country_code}"; then
188 log ERROR "Invalid wireless regulatory domain: ${country_code}"
189 return ${EXIT_ERROR}
190 fi
191
192 # Before the wireless reg domain is set, it helps to reset to 00 first.
193 if enabled reset; then
194 iw reg set 00 &>/dev/null
195 fi
196
197 log INFO "Setting wireless regulatory domain country to '${country_code}'"
198 iw reg set "${country_code}"
199 }
200
201 wireless_valid_reg_domain() {
202 local country_code="${1}"
203
204 # Empty country codes are invalid
205 isset country_code || return ${EXIT_FALSE}
206
207 local valid_country_codes="$(wireless_list_reg_domains)"
208
209 if list_match "${country_code}" ${valid_country_codes}; then
210 return ${EXIT_TRUE}
211 fi
212
213 return ${EXIT_FALSE}
214 }
215
216 wireless_list_reg_domains() {
217 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
218 log ERROR "Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
219 return ${EXIT_ERROR}
220 fi
221
222 local line
223 while read line; do
224 # Check if line starts with "country"
225 [ "${line:0:7}" = "country" ] || continue
226
227 # Print country code
228 print "${line:8:2}"
229 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
230 }
231
232 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
233 wireless_channel_to_frequency() {
234 local channel=${1}
235
236 # Works only for valid channel numbers
237 if ! wireless_channel_is_valid "${channel}"; then
238 log ERROR "Invalid wireless channel: ${channel}"
239 return ${EXIT_ERROR}
240 fi
241
242 # 2.4 GHz band
243 case "${channel}" in
244 [123456789]|1[0123])
245 print "$(( 2407 + (${channel} * 5)))"
246 return ${EXIT_OK}
247 ;;
248 14)
249 print "2484"
250 return ${EXIT_OK}
251 ;;
252 esac
253
254 # 5 GHz band
255 case "${channel}" in
256 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
257 print "$(( 5000 + (${channel} * 5)))"
258 return ${EXIT_OK}
259 ;;
260 esac
261
262 return ${EXIT_ERROR}
263 }
264
265 wireless_frequency_to_channel() {
266 local frequency=${1}
267
268 assert isinteger frequency
269
270 # Everything that is too high
271 if [ ${frequency} -gt 5825 ]; then
272 return ${EXIT_ERROR}
273
274 # 5 GHz Band
275 elif [ ${frequency} -gt 5000 ]; then
276 (( frequency = frequency - 5000 ))
277
278 # Must be divisible by 5
279 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
280
281 print "$(( frequency / 5 ))"
282
283 # 2.4 GHz Band - Channel 14
284 elif [ ${frequency} -eq 2484 ]; then
285 print "14"
286
287 # 2.4 GHz Band
288 elif [ ${frequency} -gt 2407 ]; then
289 (( frequency = frequency - 2407 ))
290
291 # Must be divisible by 5
292 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
293
294 print "$(( frequency / 5 ))"
295
296 # Everything else
297 else
298 return ${EXIT_ERROR}
299 fi
300
301 return ${EXIT_OK}
302 }
303
304 wireless_channel_is_valid() {
305 local channel=${1}
306
307 case "${channel}" in
308 # 2.4 GHz Band
309 [123456789]|1[0123]|14)
310 return ${EXIT_TRUE}
311 ;;
312
313 # 5 GHz Band
314 3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
315 return ${EXIT_TRUE}
316 ;;
317 esac
318
319 # Invalid channel number given
320 return ${EXIT_FALSE}
321 }
322
323 wireless_channel_bandwidth_is_valid() {
324 local mode="${1}"
325 assert isset mode
326
327 local bandwidth="${2}"
328 assert isset bandwidth
329
330 local bandwidths="${WIRELESS_CHANNEL_BANDWIDTHS["${mode}"]}"
331
332 list_match "${bandwidth}" ${bandwidths}
333 }
334
335 wireless_channel_is_ht40_plus() {
336 local channel="${1}"
337 assert isinteger channel
338
339 # 2.4 GHz
340 if [ ${channel} -le 6 ]; then
341 return ${EXIT_TRUE}
342 fi
343
344 return ${EXIT_FALSE}
345 }
346
347 wireless_channel_is_ht40_minus() {
348 local channel="${1}"
349 assert isinteger channel
350
351 # 2.4 GHz
352 if [ ${channel} -ge 6 ]; then
353 return ${EXIT_TRUE}
354 fi
355
356 return ${EXIT_FALSE}
357 }
358
359 wireless_set_channel() {
360 local device="${1}"
361 local channel="${2}"
362 local bandwidth="${3}"
363
364 # Check if the device exists
365 if ! device_exists "${device}"; then
366 log ERROR "No such device: ${device}"
367 return ${EXIT_ERROR}
368 fi
369
370 # Check if the channel number is valid
371 if ! wireless_channel_is_valid "${channel}"; then
372 log ERROR "Invalid wireless channel: ${channel}"
373 return ${EXIT_ERROR}
374 fi
375
376 local ht_flag
377 if [ "${bandwidth}" = "auto" ]; then
378 local phy="$(device_get_phy "${device}")"
379
380 # Offset of a 40 MHz channel
381 local ht_offset=5
382
383 if wireless_channel_is_ht40_plus "${channel}" \
384 && phy_supports_ht_capability "${phy}" "HT40+" \
385 && phy_supports_channel "${phy}" $(( channel + ht_offset )); then
386 ht_flag="HT40+"
387
388 elif wireless_channel_is_ht40_minus "${channel}" \
389 && phy_supports_ht_capability "${phy}" "HT40-" \
390 && phy_supports_channel "${phy}" $(( channel - ht_offset )); then
391 ht_flags="HT40-"
392 fi
393 fi
394
395 log DEBUG "Setting wireless channel on device '${device}' to channel '${channel}'"
396 cmd iw dev "${device}" set channel "${channel}" "${ht_flag}"
397 }
398
399 wireless_pre_shared_key_is_valid() {
400 local encryption_mode="${1}"
401 local psk="${2}"
402
403 # Length of the PSK
404 local l="${#psk}"
405
406 case "${encryption_mode}" in
407 # For WPA*, the key must be between 8 and 63 chars
408 WPA2-PSK|WPA2-PSK-SHA256|WPA-PSK|WPA-PSK-SHA256)
409 if [ ${l} -ge 8 ] && [ ${l} -le 63 ]; then
410 return ${EXIT_TRUE}
411 fi
412
413 return ${EXIT_FALSE}
414 ;;
415 esac
416
417 return ${EXIT_ERROR}
418 }
419
420 wireless_client_is_connected() {
421 local device="${1}"
422
423 device_has_carrier "${device}"
424 }
425
426 wireless_ibss_join() {
427 local device=${1}
428 assert isset device
429 shift
430
431 local bssid
432 local essid
433 local frequency
434
435 while [ $# -gt 0 ]; do
436 case "${1}" in
437 --bssid=*)
438 bssid="$(cli_get_val "${1}")"
439 ;;
440 --essid=*)
441 essid="$(cli_get_val "${1}")"
442 ;;
443 --channel=*)
444 local channel="$(cli_get_val "${1}")"
445
446 # Save the frequency of the channel instead
447 # of the channel itself.
448 if isset channel; then
449 frequency="$(wireless_channel_to_frequency ${channel})"
450 fi
451 ;;
452 esac
453 shift
454 done
455
456 # Check input.
457 assert ismac bssid
458 assert isset essid
459 assert isinteger frequency
460
461 # Set device up.
462 device_set_up "${device}"
463
464 log INFO "${device} joining ibss network: ${essid} (${bssid})"
465 cmd_quiet iw dev "${device}" ibss join "${essid}" \
466 "${frequency}" fixed-freq "${bssid}"
467 }
468
469 wireless_ibss_leave() {
470 local device=${1}
471 assert isset device
472
473 log INFO "${device} leaving ibss network"
474 cmd_quiet iw dev "${device}" ibss leave
475 }
476
477 wireless_is_radar_frequency() {
478 local frequency="${1}"
479 assert isset frequency
480
481 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
482 }
483
484 wireless_monitor() {
485 local device="${1}"
486 assert isset device
487 shift
488
489 local monitor_device="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
490
491 # Create an 802.11 monitoring device
492 wireless_create "${monitor_device}" --phy="${device}" --type="monitor"
493 local ret=$?
494
495 case "${ret}" in
496 0)
497 # Bring up the device
498 device_set_up "${monitor_device}"
499
500 # Starting tcpdump
501 tcpdump -i "${monitor_device}" "$@"
502
503 # Remove the monitoring interface.
504 wireless_remove "${monitor_device}"
505 ;;
506
507 *)
508 log ERROR "Could not create a monitoring interface on ${device}"
509 return ${EXIT_ERROR}
510 ;;
511 esac
512
513 return ${EXIT_OK}
514 }
515
516 wireless_get_ht_caps() {
517 local device="${1}"
518 assert isset device
519
520 local phy="$(device_get_phy "${device}")"
521 if ! isset phy; then
522 log ERROR "Could not determine PHY for ${device}"
523 return ${EXIT_ERROR}
524 fi
525
526 network-phy-list-ht-caps "${phy}"
527 }
528
529 wireless_get_vht_caps() {
530 local device="${1}"
531 assert isset device
532
533 local phy="$(device_get_phy "${device}")"
534 if ! isset phy; then
535 log ERROR "Could not determine PHY for ${device}"
536 return ${EXIT_ERROR}
537 fi
538
539 network-phy-list-vht-caps "${phy}"
540 }
541
542 wireless_supports_acs() {
543 local device="${1}"
544 assert isset device
545
546 local phy="$(device_get_phy "${device}")"
547 if ! isset phy; then
548 log ERROR "Could not determine PHY for ${device}"
549 return ${EXIT_ERROR}
550 fi
551
552 phy_supports_acs "${phy}"
553 }
554
555 wireless_supports_dfs() {
556 local device="${1}"
557 assert isset device
558
559 local phy="$(device_get_phy "${device}")"
560 if ! isset phy; then
561 log ERROR "Could not determine PHY for ${device}"
562 return ${EXIT_ERROR}
563 fi
564
565 phy_supports_dfs "${phy}"
566 }
567
568 wireless_environment_is_valid() {
569 local environment="${1}"
570
571 list_match "${environment}" "${WIRELESS_ENVIRONMENTS[@]}"
572 }