]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/ports/wireless-ap
hooks: Use cli_get_bool convenience function where ever possible
[people/ms/network.git] / src / hooks / ports / wireless-ap
CommitLineData
d76f5107
MT
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
8ee92277 22. /usr/lib/network/header-port
d76f5107 23
54bae947
MT
24HOOK_PORT_PATTERN="${PORT_PATTERN_ACCESSPOINT}"
25
d389e96b
MT
26HOOK_SETTINGS=(
27 "ADDRESS"
28 "BROADCAST_SSID"
29 "CHANNEL"
30 "CHANNEL_BANDWIDTH"
31 "DFS"
d389e96b 32 "ENVIRONMENT"
d389e96b
MT
33 "MFP"
34 "MODE"
35 "PHY"
0a4c5aba 36 "SECRET"
d389e96b 37 "SSID"
0a4c5aba
MT
38 "WPA3_PERSONAL"
39 "WPA2_PERSONAL"
d389e96b 40)
d76f5107 41
0a4c5aba
MT
42# Disable WPA3+2 by default
43DEFAULT_WPA3_PERSONAL="off"
44DEFAULT_WPA2_PERSONAL="off"
45
4637109c
MT
46# Broadcast SSID by default
47DEFAULT_BROADCAST_SSID="on"
d76f5107 48
7b297fb2 49# Perform radar detection by default when possible
4637109c 50DEFAULT_DFS="on"
7b297fb2 51
34ca3936 52# 802.11w - Management Frame Protection
298a1ffe 53DEFAULT_MFP="on"
34ca3936 54
4637109c 55DEFAULT_ENVIRONMENT="${WIRELESS_DEFAULT_ENVIRONMENT}"
7842c2ce 56
1c6a4e30 57hook_check_settings() {
d76f5107
MT
58 assert isset ADDRESS
59 assert ismac ADDRESS
60 assert isset BROADCAST_SSID
61 assert isbool BROADCAST_SSID
62 assert isset CHANNEL
7b297fb2 63 assert isbool DFS
34ca3936 64 assert isbool MFP
d76f5107 65 assert isset MODE
6c262922 66 assert isoneof MODE ${HOSTAPD_SUPPORTED_MODES}
d76f5107
MT
67 assert isset PHY
68 assert ismac PHY
69 assert isset SSID
25e32463 70
7842c2ce 71 assert wireless_environment_is_valid "${ENVIRONMENT}"
d76f5107
MT
72}
73
270aab39 74hook_parse_cmdline() {
d76f5107
MT
75 while [ $# -gt 0 ]; do
76 case "${1}" in
77 --broadcast-ssid=*)
2212045f 78 BROADCAST_SSID=$(cli_get_val "${1}")
d76f5107
MT
79 ;;
80 --channel=*)
2212045f 81 CHANNEL=$(cli_get_val "${1}")
d76f5107 82 ;;
54094fc7
MT
83 --channel-bandwidth=*)
84 CHANNEL_BANDWIDTH="$(cli_get_val "${1}")"
85 ;;
7b297fb2 86 --dfs=*)
f6659cc5 87 DFS="$(cli_get_bool "${1}")"
7b297fb2 88 ;;
7842c2ce
MT
89 --environment=*)
90 ENVIRONMENT="$(cli_get_val "${1}")"
91
92 if ! wireless_environment_is_valid "${ENVIRONMENT}"; then
93 error "Invalid wireless environment: ${ENVIRONMENT}"
94 return ${EXIT_ERROR}
95 fi
96 ;;
d76f5107 97 --mac=*)
2212045f 98 ADDRESS=$(cli_get_val "${1}")
d76f5107 99 ;;
34ca3936 100 --mfp=*)
f6659cc5 101 MFP="$(cli_get_bool "${1}")"
34ca3936 102 ;;
d76f5107 103 --mode=*)
2212045f 104 MODE=$(cli_get_val "${1}")
6c262922
MT
105
106 if ! isoneof MODE ${HOSTAPD_SUPPORTED_MODES}; then
107 error "Unsupported mode: ${MODE}"
108 error "Mode must be one of ${HOSTAPD_SUPPORTED_MODES}"
109 return ${EXIT_ERROR}
110 fi
d76f5107
MT
111 ;;
112 --phy=*)
2212045f 113 PHY=$(cli_get_val "${1}")
d76f5107 114 ;;
0a4c5aba
MT
115 --secret=*)
116 SECRET="$(cli_get_val "${1}")"
117 ;;
d76f5107 118 --ssid=*)
2212045f 119 SSID=$(cli_get_val "${1}")
d76f5107 120 ;;
0a4c5aba
MT
121 --wpa2-personal=*)
122 WPA2_PERSONAL="$(cli_get_bool "${1}")"
123 ;;
124 --wpa3-personal=*)
125 WPA3_PERSONAL="$(cli_get_bool "${1}")"
126 ;;
d76f5107
MT
127 *)
128 warning "Ignoring unknown argument '${1}'"
129 ;;
130 esac
131 shift
132 done
133
8578e61d
MT
134 # Generate a random MAC address if none is set
135 if ! isset ADDRESS; then
136 ADDRESS="$(mac_generate)"
137 fi
138
6c262922
MT
139 # MODE must be set
140 if ! isset MODE; then
141 error "--mode is not set"
142 return ${EXIT_ERROR}
143 fi
144
1b4aa2ca
MT
145 # Automatically enable ACS if no channel is set and ACS is available
146 if ! isset CHANNEL && phy_supports_acs "${PHY}"; then
147 CHANNEL="0"
148
149 log INFO "Automatic Channel Selection (ACS) enabled"
150 fi
151
54094fc7
MT
152 # Channel bandwidth must match the mode
153 if isset CHANNEL_BANDWIDTH && ! wireless_channel_bandwidth_is_valid "${MODE}" "${CHANNEL_BANDWIDTH}"; then
f9e980d9 154 error "Channel Bandwidth '${CHANNEL_BANDWIDTH}' is not supported for ${MODE}"
54094fc7
MT
155 return ${EXIT_ERROR}
156 fi
157
0a4c5aba
MT
158 # Check if SECRET is set when WPA* is enabled
159 if ! isset SECRET && (enabled WPA3_PERSONAL || enabled WPA2_PERSONAL); then
160 error "Secret is not set when PSK authentication is enabled"
161 return ${EXIT_ERROR}
162 fi
163
d76f5107
MT
164 # Save address of phy do identify it again
165 PHY=$(phy_get ${PHY})
166 PHY=$(phy_get_address ${PHY})
270aab39
MT
167}
168
1c6a4e30 169hook_edit() {
d76f5107 170 local port=${1}
d76f5107
MT
171 assert isset port
172
2212045f 173 if ! hook_default_edit "$@"; then
270aab39
MT
174 return ${EXIT_ERROR}
175 fi
d76f5107 176
270aab39
MT
177 # To apply all changes, we need to restart the port
178 port_restart "${port}"
d76f5107
MT
179}
180
1c6a4e30 181hook_create() {
1ba6a2bb 182 local port="${1}"
d76f5107
MT
183 assert isset port
184
1ba6a2bb
MT
185 device_exists "${port}" && exit ${EXIT_OK}
186
eba9fa9c 187 port_settings_read "${port}"
d76f5107 188
49ec20d8
MT
189 # Check if the PHY is present.
190 local phy=$(phy_get ${PHY})
191 if ! isset phy; then
192 log DEBUG "phy '${PHY}' is not present"
193 exit ${EXIT_ERROR}
194 fi
195
1ba6a2bb
MT
196 # Create the wireless device
197 wireless_create "${port}" \
198 --phy="${phy}" \
199 --type="ap" \
200 --address="${ADDRESS}"
d76f5107
MT
201
202 exit ${EXIT_OK}
203}
204
1c6a4e30 205hook_remove() {
1ba6a2bb 206 local port="${1}"
d76f5107
MT
207 assert isset port
208
b8026986
MT
209 # Remove the device if present
210 if device_exists "${port}"; then
211 wireless_remove "${port}"
47859d95 212 fi
d76f5107
MT
213
214 exit ${EXIT_OK}
215}
216
1c6a4e30 217hook_up() {
1ba6a2bb
MT
218 local port="${1}"
219 assert isset port
220
221 # The port must already exist before
222 # hostapd is started. Otherwise it will
223 # fail horribly over and over again.
224 assert device_exists "${port}"
225
226 hostapd_start "${port}"
227}
228
1c6a4e30 229hook_down() {
1ba6a2bb
MT
230 local port="${1}"
231 assert isset port
232
233 hostapd_stop "${port}"
234}
235
1c6a4e30 236hook_hotplug() {
b8026986 237 local port="${1}"
47859d95 238 assert isset port
49ec20d8 239
b8026986
MT
240 case "$(hotplug_action)" in
241 add)
1ba6a2bb
MT
242 # Create the port when the phy is plugged in
243 if hotplug_event_port_uses_phy "${port}"; then
244 hook_create "${port}"
b8026986
MT
245 fi
246 ;;
247
248 remove)
249 # Stop hostapd
250 if hotplug_event_port_is_interface "${port}"; then
251 hostapd_stop "${port}"
b8026986 252
1ba6a2bb
MT
253 exit ${EXIT_OK}
254 fi
b8026986
MT
255 ;;
256 esac
47859d95 257
1ba6a2bb 258 exit ${EXIT_NOT_HANDLED}
47859d95 259}