]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/zones/wireless
593b37c45166c757af871bdbe0b494727d035ad8
[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 PHY MAC SSID KEY ENCRYPTION_MODE"
25
26 # Default values
27 ADDRESS=$(mac_generate)
28 PHY=
29 SSID=
30 KEY=
31 ENCRYPTION_MODE=
32
33 hook_check_settings() {
34 assert isset SSID
35
36 if isset ADDRESS; then
37 assert ismac ADDRESS
38 fi
39
40 assert ismac PHY
41
42 if [ -n "${ENCRYPTION_MODE}" ]; then
43 assert isset KEY
44 fi
45 }
46
47 hook_parse_cmdline() {
48 while [ $# -gt 0 ]; do
49 case "${1}" in
50 --phy=*|--parent-device=*)
51 PHY=$(cli_get_val "${1}")
52 ;;
53 --encryption-mode=*)
54 ENCRYPTION_MODE=$(cli_get_val "${1}")
55 ;;
56 --address=*)
57 ADDRESS=$(cli_get_val "${1}")
58 ;;
59 --ssid=*)
60 SSID=$(cli_get_val "${1}")
61 ;;
62 --key=*)
63 KEY=$(cli_get_val "${1}")
64 ;;
65 *)
66 warning "Unrecognized option: ${1}"
67 ;;
68 esac
69 shift
70 done
71
72 # Just save the MAC address of the phy.
73 PHY=$(phy_get ${PHY})
74 PHY=$(phy_get_address ${PHY})
75 }
76
77 hook_up() {
78 local zone=${1}
79 assert isset zone
80
81 # Read zone configuration.
82 zone_settings_read "${zone}"
83
84 if ! device_exists ${zone}; then
85 # Create the wireless interface.
86 wireless_create ${zone} \
87 --phy=${PHY} \
88 --type="managed" \
89 --address="${ADDRESS}" \
90 || exit $?
91 fi
92
93 # Write WPA supplicant configuration
94 wpa_supplicant_config_write "${zone}" \
95 --mode="${ENCRYPTION_MODE}" \
96 --ssid="${SSID}" \
97 --key="${KEY}" || return $?
98
99 # Start the WPA supplicant daemon.
100 wpa_supplicant_start ${zone}
101
102 zone_configs_up ${zone}
103
104 exit ${EXIT_OK}
105 }
106
107 hook_down() {
108 local zone=${1}
109 shift
110
111 if ! device_is_up ${zone}; then
112 warning "Zone '${zone}' is not up"
113 exit ${EXIT_OK}
114 fi
115
116 zone_configs_down ${zone}
117
118 wpa_supplicant_stop ${zone}
119
120 # Remove WPA supplicant configuration
121 wpa_supplicant_config_destroy "${zone}"
122
123 wireless_remove ${zone}
124
125 exit ${EXIT_OK}
126 }
127
128 hook_status() {
129 local zone=${1}
130 assert isset zone
131
132 # Print the default header.
133 cli_device_headline ${zone}
134
135 # Exit if zone is down
136 if ! zone_is_up ${zone}; then
137 echo # Empty line
138 exit ${EXIT_ERROR}
139 fi
140
141 cli_headline 2 "Wireless network information"
142 cli_print_fmt1 2 "SSID" "$(wpa_cli_status_get ${zone} ssid)"
143 cli_space
144
145 cli_headline 3 "Access Point"
146 local bssid=$(wpa_cli_status_get ${zone} bssid)
147 assert isset bssid
148
149 local frequency=$(wpa_cli_bss_get_frequency "${zone}" "${bssid}")
150 cli_print_fmt1 3 "Channel" "$(wireless_frequency_to_channel ${frequency}) (${frequency} MHz)"
151 cli_print_fmt1 3 "BSSID" "${bssid}"
152 cli_print_fmt1 3 "Noise" \
153 "$(wpa_cli_bss_get_noise ${zone} ${bssid})"
154 cli_print_fmt1 3 "Quality" \
155 "$(wpa_cli_bss_get_quality ${zone} ${bssid})%%"
156 cli_print_fmt1 3 "Flags" \
157 "$(wpa_cli_bss_get_flags ${zone} ${bssid})"
158 cli_space
159
160 cli_headline 3 "Encryption"
161 cli_print_fmt1 3 "Mode" \
162 "$(wpa_cli_status_get ${zone} key_mgmt)"
163 cli_print_fmt1 3 "Pairwise cipher" \
164 "$(wpa_cli_status_get ${zone} pairwise_cipher)"
165 cli_print_fmt1 3 "Group cipher" \
166 "$(wpa_cli_status_get ${zone} group_cipher)"
167 cli_space
168
169 cli_headline 2 "Configurations"
170 zone_configs_cmd status ${zone}
171 cli_space
172
173 exit ${EXIT_OK}
174 }