]> git.ipfire.org Git - people/stevee/network.git/blobdiff - functions.wireless
wireless: Add function to find DFS channels.
[people/stevee/network.git] / functions.wireless
index eacc6abfcc0384b09196635fb2342ec44168784b..cd17de4cf97d99568933b3137f1c89005d91b09b 100644 (file)
 
 function wireless_create() {
        local device=${1}
-       local phy=$(phy_get ${2})
-       local type=${3}
-       local mac=${4}
-
        assert isset device
-       assert isset phy
-       assert isset type
-
-       isset mac || mac=$(mac_generate)
-
-       assert phy_exists ${phy}
-       assert isoneof type managed __ap
-
-       iw phy ${phy} interface add ${device} type ${type}
-
-       if device_exists ${device}; then
-               device_set_address ${device} ${mac}
-       fi
-
-       device_set_up ${device}
-}
-
-function wireless_remove() {
-       local device=${1}
-
-       assert device_exists ${device}
-
-       device_set_down ${device}
-
-       iw dev ${device} del
-}
-
-function wireless_set_channel() {
-       local device=${1}
-       local channel=${2}
-
-       assert isset device
-       assert device_exists ${device}
-       assert isset channel
-
-       iw dev ${device} set channel ${channel} $@
-}
-
-function wpa_supplicant_config_write() {
-       local device=${1}
        shift
 
-       assert isset device
-
-       local ssid
-       local encryption
-       local key
+       local address
+       local phy
+       local type="managed"
 
        while [ $# -gt 0 ]; do
                case "${1}" in
-                       --ssid=*)
-                               ssid=${1#--ssid=}
+                       --address=*)
+                               address=$(cli_get_val ${1})
                                ;;
-                       --encryption=*)
-                               encryption=${1#--encryption=}
+                       --phy=*)
+                               phy=$(cli_get_val ${1})
+                               phy=$(phy_get ${phy})
                                ;;
-                       --key=*)
-                               key=${1#--key=}
+                       --type=*)
+                               type=$(cli_get_val ${1})
+
+                               # ap --> __ap
+                               [ "${type}" = "ap" ] && type="__ap"
+                               ;;
+                       *)
+                               error "Unrecognized argument: ${1}"
+                               return ${EXIT_ERROR}
                                ;;
                esac
                shift
        done
 
-       assert isset ssid
-       assert isset encryption
-       assert isset key
-
-       cat <<EOF
-# WPA supplicant configuration for ${device}.
-# DO NOT EDIT.
-
-network={
-       ssid="${ssid}"
-       proto=RSN
-       key_mgmt=${encryption}
-       pairwise=CCMP
-       group=TKIP
-       psk="${key}"
-}
+       assert isoneof type ibss managed __ap
+       assert phy_exists ${phy}
+       isset address || address=$(mac_generate)
 
-EOF
-}
+       cmd_quiet iw phy ${phy} interface add ${device} type ${type}
+       local ret=$?
 
-function wpa_supplicant_config_dir() {
-       local device=${1}
+       if [ ${ret} -eq ${EXIT_OK} ]; then
+               log DEBUG "created wireless device '${device}' (${type})"
 
-       assert isset device
+               if isset address; then
+                       device_set_address ${device} ${address}
+               fi
+       else
+               log ERROR "could not create wireless device '${device}' (${type}): ${ret}"
+       fi
 
-       echo "${RUN_DIR}/wireless/${device}"
+       return ${ret}
 }
 
-function wpa_supplicant_start() {
+function wireless_remove() {
        local device=${1}
-       shift
+       assert isset device
 
-       assert device_exists ${device}
+       if ! device_exists ${device}; then
+               return ${EXIT_OK}
+       fi
 
-       local config_dir=$(wpa_supplicant_config_dir ${device})
-       mkdir -p ${config_dir}
+       # Tear down the device (if necessary).
+       device_set_down ${device}
 
-       local config_file=${config_dir}/config
-       wpa_supplicant_config_write ${device} $@ > ${config_file}
+       # Remove it.
+       cmd_quiet iw dev ${device} del
+       local ret=$?
 
-       wpa_supplicant -i ${device} -D wext -B -c ${config_file} \
-               -P ${config_dir}/pid
-}
+       if [ ${ret} -eq ${EXIT_OK} ]; then
+               log DEBUG "removed wireless device '${device}'"
+       else
+               log ERROR "could not remove wireless device '${device}': ${ret}"
+       fi
 
-function wpa_supplicant_stop() {
-       local device=${1}
+       return ${ret}
+}
 
-       assert isset device
+function wireless_channel_to_frequency() {
+       # http://en.wikipedia.org/wiki/List_of_WLAN_channels
 
-       local pid=$(wpa_supplicant_get_pid ${device})
+       local channel=${1}
+       assert isset channel
 
-       if isset pid; then
-               process_kill ${pid}
-       else
-               warning_log "Could not find pid file for wpa_supplicant process running for ${device}."
-       fi
+       # Channel number must be positive.
+       assert [ "${channel}" -gt 0 ]
+
+       # 2.4 GHz band
+       case "${channel}" in
+               [123456789]|1[0123])
+                       print "$(( 2407 + (${channel} * 5)))"
+                       return ${EXIT_OK}
+                       ;;
+               14)
+                       print "2484"
+                       return ${EXIT_OK}
+                       ;;
+       esac
+
+       # 5 GHz band
+       case "${channel}" in
+               3[68]|4[02468]|5[26]|6[04]|10[048]|11[26]|12[048]|13[26]|14[09]|15[37]|16[15])
+                       print "$(( 5000 + (${channel} * 5)))"
+                       return ${EXIT_OK}
+                       ;;
+       esac
 
-       rm -rf $(wpa_supplicant_config_dir ${device})
+       return ${EXIT_ERROR}
 }
 
-function wpa_supplicant_get_pid() {
+function wireless_set_channel() {
        local device=${1}
-
        assert isset device
 
-       local pid_file="$(wpa_supplicant_config_dir ${device})/pid"
+       local channel=${2}
+       assert isset channel
 
-       [ -e "${pid_file}" ] || return ${EXIT_ERROR}
+       device_exists ${device} || return ${EXIT_ERROR}
 
-       cat ${pid_file} 2>/dev/null
-       return ${EXIT_OK}
+       cmd_quiet iw dev ${device} set channel ${channel}
 }
 
-function wpa_supplicant_is_running() {
+function wireless_ibss_join() {
        local device=${1}
-
        assert isset device
+       shift
 
-       local pid=$(wpa_supplicant_get_pid ${device})
+       local bssid
+       local essid
+       local frequency
 
-       if isset pid && [ -d "/proc/${pid}" ]; then
-               return ${EXIT_OK}
-       fi
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --bssid=*)
+                               bssid="$(cli_get_val ${1})"
+                               ;;
+                       --essid=*)
+                               essid="$(cli_get_val ${1})"
+                               ;;
+                       --channel=*)
+                               local channel="$(cli_get_val ${1})"
+
+                               # Save the frequency of the channel instead
+                               # of the channel itself.
+                               if isset channel; then
+                                       frequency="$(wireless_channel_to_frequency ${channel})"
+                               fi
+                               ;;
+               esac
+               shift
+       done
 
-       return ${EXIT_ERROR}
+       # Check input.
+       assert ismac bssid
+       assert isset essid
+       assert isinteger frequency
+
+       # Set device up.
+       device_set_up "${device}"
+
+       log INFO "${device} joining ibss network: ${essid} (${bssid})"
+       cmd_quiet iw dev "${device}" ibss join "${essid}" \
+               "${frequency}" fixed-freq "${bssid}"
 }
 
-function wpa_supplicant_get_pid() {
-       local zone=${1}
-       shift
+function wireless_ibss_leave() {
+       local device=${1}
+       assert isset device
 
-       
+       log INFO "${device} leaving ibss network"
+       cmd_quiet iw dev "${device}" ibss leave
 }
 
-function wpa_supplicant_stop() {
-       local zone=${1}
-       shift
+function wireless_is_radar_frequency() {
+       local frequency="${1}"
+       assert isset frequency
 
-       killall wpa_supplicant
+       [[ ${frequency} -ge 5260 ]] && [[ ${frequency} -le 5700 ]]
 }