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"
28 WIRELESS_NETWORK_CONFIG_SETTINGS
="\
34 WIRELESS_NETWORKS_VALID_ENCRYPTION_MODES
="WPA2-PSK"
46 while [ $# -gt 0 ]; do
49 address
=$
(cli_get_val
"${1}")
52 channel
=$
(cli_get_val
"${1}")
55 phy
=$
(cli_get_val
"${1}")
59 type=$
(cli_get_val
"${1}")
62 [ "${type}" = "ap" ] && type="__ap"
65 error
"Unrecognized argument: ${1}"
73 ibss|managed|monitor|__ap
)
79 log ERROR
"Unknown type: ${type}"
85 assert phy_exists
${phy}
86 isset address || address
=$
(mac_generate
)
88 cmd_quiet iw phy
${phy} interface add ${device} type ${type}
91 if [ ${ret} -eq ${EXIT_OK} ]; then
92 log DEBUG
"created wireless device '${device}' (${type})"
94 if isset address
; then
95 device_set_address
${device} ${address}
98 log ERROR
"could not create wireless device '${device}' (${type}): ${ret}"
102 if isset channel
; then
103 wireless_set_channel
"${device}" "${channel}" ||
return $?
113 if ! device_exists
${device}; then
117 # Tear down the device (if necessary).
118 device_set_down
${device}
121 cmd_quiet iw dev
${device} del
124 if [ ${ret} -eq ${EXIT_OK} ]; then
125 log DEBUG
"removed wireless device '${device}'"
127 log ERROR
"could not remove wireless device '${device}': ${ret}"
133 wireless_get_reg_domain
() {
134 # Returns the country code for the wireless device.
135 # Defaults to 00 = world if unset.
136 print
"${WIRELESS_REGULATORY_DOMAIN:-00}"
139 wireless_init_reg_domain
() {
140 local country_code
="$(wireless_get_reg_domain)"
142 wireless_set_reg_domain
"${country_code}" --no-reset
145 wireless_set_reg_domain
() {
149 while [ $# -gt 0 ]; do
155 log ERROR
"Ignoring invalid option: ${1}"
164 # Check if configuration value is valid
165 if ! wireless_valid_reg_domain
"${country_code}"; then
166 log ERROR
"Invalid wireless regulatory domain: ${country_code}"
170 # Before the wireless reg domain is set, it helps to reset to 00 first.
171 if enabled
reset; then
172 iw reg
set 00 &>/dev
/null
175 log INFO
"Setting wireless regulatory domain country to '${country_code}'"
176 iw reg
set "${country_code}"
179 wireless_valid_reg_domain
() {
180 local country_code
="${1}"
182 # Empty country codes are invalid
183 isset country_code ||
return ${EXIT_FALSE}
185 local valid_country_codes
="$(wireless_list_reg_domains)"
187 if list_match
"${country_code}" ${valid_country_codes}; then
194 wireless_list_reg_domains
() {
195 if [ ! -r "${WIRELESS_REGULATORY_DOMAIN_DATABASE}" ]; then
196 log ERROR
"Could not read ${WIRELESS_REGULATORY_DOMAIN_DATABASE}"
202 # Check if line starts with "country"
203 [ "${line:0:7}" = "country" ] ||
continue
207 done <<< "$(regdbdump ${WIRELESS_REGULATORY_DOMAIN_DATABASE})"
210 # http://en.wikipedia.org/wiki/List_of_WLAN_channels
211 wireless_channel_to_frequency
() {
214 # Works only for valid channel numbers
215 if ! wireless_channel_is_valid
"${channel}"; then
216 log ERROR
"Invalid wireless channel: ${channel}"
223 print
"$(( 2407 + (${channel} * 5)))"
234 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
235 print
"$(( 5000 + (${channel} * 5)))"
243 wireless_frequency_to_channel
() {
246 assert isinteger frequency
248 # Everything that is too high
249 if [ ${frequency} -gt 5825 ]; then
253 elif [ ${frequency} -gt 5000 ]; then
254 (( frequency
= frequency
- 5000 ))
256 # Must be divisible by 5
257 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
259 print
"$(( frequency / 5 ))"
261 # 2.4 GHz Band - Channel 14
262 elif [ ${frequency} -eq 2484 ]; then
266 elif [ ${frequency} -gt 2407 ]; then
267 (( frequency
= frequency
- 2407 ))
269 # Must be divisible by 5
270 [ "$(( frequency % 5 ))" -ne 0 ] && return ${EXIT_ERROR}
272 print
"$(( frequency / 5 ))"
282 wireless_channel_is_valid
() {
287 [123456789]|
1[0123]|
14)
292 3[68]|
4[02468]|
5[26]|
6[04]|
10[048]|
11[26]|
12[048]|
13[26]|
14[09]|
15[37]|
16[15])
297 # Invalid channel number given
301 wireless_set_channel
() {
305 # Check if the device exists
306 if ! device_exists
"${device}"; then
307 log ERROR
"No such device: ${device}"
311 # Check if the channel number is valid
312 if ! wireless_channel_is_valid
"${channel}"; then
313 log ERROR
"Invalid wireless channel: ${channel}"
317 log DEBUG
"Setting wireless channel on device '${device}' to channel '${channel}'"
318 cmd iw dev
"${device}" set channel
"${channel}"
321 wireless_ibss_join
() {
330 while [ $# -gt 0 ]; do
333 bssid
="$(cli_get_val "${1}")"
336 essid
="$(cli_get_val "${1}")"
339 local channel
="$(cli_get_val "${1}")"
341 # Save the frequency of the channel instead
342 # of the channel itself.
343 if isset channel
; then
344 frequency
="$(wireless_channel_to_frequency ${channel})"
354 assert isinteger frequency
357 device_set_up
"${device}"
359 log INFO
"${device} joining ibss network: ${essid} (${bssid})"
360 cmd_quiet iw dev
"${device}" ibss
join "${essid}" \
361 "${frequency}" fixed-freq
"${bssid}"
364 wireless_ibss_leave
() {
368 log INFO
"${device} leaving ibss network"
369 cmd_quiet iw dev
"${device}" ibss leave
372 wireless_is_radar_frequency
() {
373 local frequency
="${1}"
374 assert isset frequency
376 [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
384 local monitor_device
="$(port_find_free "${PORT_PATTERN_WIRELESS_MONITOR}")"
386 # Create an 802.11 monitoring device
387 wireless_create
"${monitor_device}" --phy="${device}" --type="monitor"
392 # Bring up the device
393 device_set_up
"${monitor_device}"
396 tcpdump
-i "${monitor_device}" "$@"
398 # Remove the monitoring interface.
399 wireless_remove
"${monitor_device}"
403 log ERROR
"Could not create a monitoring interface on ${device}"
417 cli_wireless_network
"$@"
420 error
"Unrecognized argument: ${action}"
426 cli_wireless_network
() {
427 if wireless_network_exists
"${1}"; then
434 encryption_mode|key|priority
)
435 wireless_network_
${key} "${ssid}" "$@"
438 wireless_network_show
"${ssid}"
442 error
"Unrecognized argument: ${key}"
452 wireless_network_new
"$@"
455 wireless_network_destroy
"$@"
458 if [ -n "${action}" ]; then
459 error
"Unrecognized argument: '${action}'"
467 # This function writes all values to a via ${ssid} specificated wireless network configuration file
468 wireless_network_write_config
() {
473 local ssid_hash
="$(wireless_network_hash "${ssid}")"
474 assert isset ssid_hash
476 if ! wireless_network_exists
"${ssid}"; then
477 log ERROR
"No such wireless network: '${ssid}'"
481 local path
="${NETWORK_WIRELESS_NETWORK_DIR}/${ssid_hash}/settings"
483 if ! settings_write
"${path}" ${WIRELESS_NETWORK_CONFIG_SETTINGS}; then
484 log ERROR
"Could not write configuration settings for wireless network ${ssid}"
488 # When we get here the writing of the config file was successful
492 # This funtion writes the value for one key to a via ${ssid} specificated
493 # wireless network configuration file
494 wireless_network_write_config_key
() {
503 if ! wireless_network_exists
"${ssid}"; then
504 log ERROR
"No such wireless network: ${ssid}"
508 log DEBUG
"Set '${key}' to new value '${value}' in wireless network '${ssid}'"
510 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
512 # Read the config settings
513 if ! wireless_network_read_config
"${ssid}"; then
517 # Set the key to a new value
518 assign
"${key}" "${value}"
520 if ! wireless_network_write_config
"${ssid}"; then
527 # Reads one or more keys out of a settings file or all if no key is provided.
528 wireless_network_read_config
() {
534 local ssid_hash
="$(wireless_network_hash "${ssid}")"
535 assert isset ssid_hash
537 if ! wireless_network_exists
"${ssid}"; then
538 log ERROR
"No such wireless network : ${ssid}"
543 if [ $# -eq 0 ] && [ -n "${WIRELESS_NETWORK_CONFIG_SETTINGS}" ]; then
544 list_append args
${WIRELESS_NETWORK_CONFIG_SETTINGS}
546 list_append args
"$@"
549 local path
="${NETWORK_WIRELESS_NETWORK_DIR}/${ssid_hash}/settings"
551 if ! settings_read
"${path}" ${args}; then
552 log ERROR
"Could not read settings for wireless network ${ssid}"
557 # This function checks if a wireless network exists
558 # Returns True when yes and false when not
559 wireless_network_exists
() {
563 local ssid_hash
="$(wireless_network_hash "${ssid}")"
564 assert isset ssid_hash
566 local path
="${NETWORK_WIRELESS_NETWORK_DIR}/${ssid_hash}"
568 # We cannot use wireless_network_read_config here beacuse we would end in a loop
571 local path_settings
="${NETWORK_WIRELESS_NETWORK_DIR}/${ssid_hash}/settings"
573 if ! settings_read
"${path_settings}" SSID
; then
579 if [ -d "${path}" ] && [[ "${ssid}" = "${SSID}" ]]; then
586 wireless_network_hash
() {
591 local hash=$
(echo -n "${string}" |
md5sum )
594 local path
="${NETWORK_WIRELESS_NETWORK_DIR}/*${hash}"
596 if [ -d "${path}" ]; then
599 local normalized
=$
(normalize
"${string}")
600 normalized
=${normalized%-}
601 echo "${normalized}-${hash}"
605 wireless_network_new
() {
606 if [ $# -gt 1 ]; then
607 error
"Too many arguments"
612 if ! isset ssid
; then
613 error
"Please provide a SSID"
617 local ssid_hash
="$(wireless_network_hash "${ssid}")"
618 assert isset ssid_hash
620 # Check for duplicates
621 if wireless_network_exists
"${ssid}"; then
622 error
"The wireless network ${ssid} already exists"
626 log DEBUG
"Creating wireless network '${ssid}'"
628 if ! mkdir
-p "${NETWORK_WIRELESS_NETWORK_DIR}/${ssid_hash}"; then
629 log ERROR
"Could not create config directory for wireless network ${ssid}"
633 # When the ssid is not set in the settings file we cannot write it because wireless_network_exists fails
634 echo "SSID=\"${ssid}\"" >>"${NETWORK_WIRELESS_NETWORK_DIR}/${ssid_hash}/settings"
636 local ${WIRELESS_NETWORK_CONFIG_SETTINGS}
640 if ! wireless_network_write_config
"${ssid}"; then
641 log ERROR
"Could not write new config file"
646 # Function that deletes based on the passed parameters
647 # one ore more wireless networks
648 wireless_network_destroy
() {
651 local ssid_hash
="$(wireless_network_hash "${ssid}")"
652 assert isset ssid_hash
654 if ! wireless_network_exists
"${ssid}"; then
655 log ERROR
"The wireless network ${ssid} does not exist."
659 log DEBUG
"Deleting wireless network ${ssid}"
661 if ! rm -rf "${NETWORK_WIRELESS_NETWORK_DIR}/${ssid_hash}"; then
662 log ERROR
"Deleting the wireless network ${ssid} was not sucessful"
668 wireless_network_encryption_mode
() {
669 if [ ! $# -eq 2 ]; then
670 log ERROR
"Not enough arguments"
676 if ! isoneof mode
${WIRELESS_NETWORKS_VALID_ENCRYPTION_MODES}; then
677 log ERROR
"Encryption mode '${mode}' is invalid"
681 if ! wireless_network_write_config_key
"${ssid}" "ENCRYPTION_MODE" ${mode^^}; then
682 log ERROR
"Could not write configuration settings"
687 wireless_network_key
() {
688 if [ ! $# -eq 2 ]; then
689 log ERROR
"Not enough arguments"
695 if ! wireless_network_write_config_key
"${ssid}" "KEY" "${key}"; then
696 log ERROR
"Could not write configuration settings"
701 wireless_network_priority
() {
702 if [ ! $# -eq 2 ]; then
703 log ERROR
"Not enough arguments"
709 if ! isinteger priority
&& [ ! ${priority} -ge 0 ]; then
710 log ERROR
"The priority must be an integer greater or eqal zero"
714 if ! wireless_network_write_config_key
"${ssid}" "PRIORITY" "${priority}"; then
715 log ERROR
"Could not write configuration settings"