wireless: Fix crash of status if not connected
[people/stevee/network.git] / src / hooks / zones / wireless
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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 . /usr/lib/network/header-zone
23
24 HOOK_SETTINGS="HOOK ADDRESS PHY"
25
26 hook_check_settings() {
27 assert ismac ADDRESS
28 assert ismac PHY
29 }
30
31 hook_parse_cmdline() {
32 while [ $# -gt 0 ]; do
33 case "${1}" in
34 --address=*)
35 ADDRESS=$(cli_get_val "${1}")
36 ;;
37 --phy=*)
38 PHY=$(cli_get_val "${1}")
39 ;;
40 *)
41 warning "Unrecognized option: ${1}"
42 ;;
43 esac
44 shift
45 done
46
47 # Just save the MAC address of the phy.
48 PHY=$(phy_get ${PHY})
49 PHY=$(phy_get_address ${PHY})
50
51 # Generate a random MAC address if none given
52 if ! isset ADDRESS; then
53 ADDRESS="$(mac_generate)"
54 fi
55 }
56
57 hook_up() {
58 local zone=${1}
59 assert isset zone
60
61 # Read zone configuration.
62 zone_settings_read "${zone}"
63
64 # Create the wireless interface
65 if ! device_exists "${zone}"; then
66 wireless_create "${zone}" \
67 --phy=${PHY} \
68 --type="managed" \
69 --address="${ADDRESS}" \
70 || return $?
71 fi
72
73 # Write WPA supplicant configuration
74 if ! wireless_networks_write_wpa_supplicant_configuration "${zone}"; then
75 log ERROR "Could not write WPA supplicant configuration for ${zone}"
76 return ${EXIT_ERROR}
77 fi
78
79 # Start the WPA supplicant daemon.
80 wpa_supplicant_start ${zone}
81
82 zone_configs_up ${zone}
83
84 return ${EXIT_OK}
85 }
86
87 hook_down() {
88 local zone=${1}
89 shift
90
91 if ! device_is_up ${zone}; then
92 warning "Zone '${zone}' is not up"
93 exit ${EXIT_OK}
94 fi
95
96 zone_configs_down ${zone}
97
98 wpa_supplicant_stop ${zone}
99
100 # Remove WPA supplicant configuration
101 wpa_supplicant_config_destroy "${zone}"
102
103 wireless_remove ${zone}
104
105 exit ${EXIT_OK}
106 }
107
108 hook_status() {
109 local zone=${1}
110 assert isset zone
111
112 # Print the default header.
113 cli_device_headline ${zone}
114
115 # Exit if zone is down
116 if ! zone_is_up ${zone}; then
117 echo # Empty line
118 exit ${EXIT_ERROR}
119 fi
120
121 if wireless_client_is_connected "${zone}"; then
122 cli_headline 2 "Wireless network information"
123 cli_print_fmt1 2 "SSID" "$(wpa_cli_status_get ${zone} ssid)"
124 cli_space
125
126 cli_headline 3 "Access Point"
127 local bssid=$(wpa_cli_status_get ${zone} bssid)
128 assert isset bssid
129
130 local frequency=$(wpa_cli_bss_get_frequency "${zone}" "${bssid}")
131 cli_print_fmt1 3 "Channel" "$(wireless_frequency_to_channel ${frequency}) (${frequency} MHz)"
132 cli_print_fmt1 3 "BSSID" "${bssid}"
133 cli_print_fmt1 3 "Noise" \
134 "$(wpa_cli_bss_get_noise ${zone} ${bssid})"
135 cli_print_fmt1 3 "Quality" \
136 "$(wpa_cli_bss_get_quality ${zone} ${bssid})%%"
137 cli_print_fmt1 3 "Flags" \
138 "$(wpa_cli_bss_get_flags ${zone} ${bssid})"
139 cli_space
140
141 cli_headline 3 "Encryption"
142 cli_print_fmt1 3 "Mode" \
143 "$(wpa_cli_status_get ${zone} key_mgmt)"
144 cli_print_fmt1 3 "Pairwise cipher" \
145 "$(wpa_cli_status_get ${zone} pairwise_cipher)"
146 cli_print_fmt1 3 "Group cipher" \
147 "$(wpa_cli_status_get ${zone} group_cipher)"
148 cli_space
149 fi
150
151 cli_headline 2 "Configurations"
152 zone_configs_cmd status ${zone}
153 cli_space
154
155 exit ${EXIT_OK}
156 }