]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/ports/wireless-ap
wireless-ap: Check that secret has the correct length and no invalid characters
[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 158 # Check if SECRET is set when WPA* is enabled
d695b280
MT
159 if enabled WPA3_PERSONAL || enabled WPA2_PERSONAL; then
160 if ! isset SECRET; then
161 error "Secret is not set when PSK authentication is enabled"
162 return ${EXIT_ERROR}
163 fi
164
165 # Check if SECRET is valid
166 if ! wireless_pre_shared_key_is_valid "${SECRET}"; then
167 error "The secret is in an invalid format"
168 return ${EXIT_ERROR}
169 fi
0a4c5aba
MT
170 fi
171
d76f5107
MT
172 # Save address of phy do identify it again
173 PHY=$(phy_get ${PHY})
174 PHY=$(phy_get_address ${PHY})
270aab39
MT
175}
176
1c6a4e30 177hook_edit() {
d76f5107 178 local port=${1}
d76f5107
MT
179 assert isset port
180
2212045f 181 if ! hook_default_edit "$@"; then
270aab39
MT
182 return ${EXIT_ERROR}
183 fi
d76f5107 184
270aab39
MT
185 # To apply all changes, we need to restart the port
186 port_restart "${port}"
d76f5107
MT
187}
188
1c6a4e30 189hook_create() {
1ba6a2bb 190 local port="${1}"
d76f5107
MT
191 assert isset port
192
1ba6a2bb
MT
193 device_exists "${port}" && exit ${EXIT_OK}
194
eba9fa9c 195 port_settings_read "${port}"
d76f5107 196
49ec20d8
MT
197 # Check if the PHY is present.
198 local phy=$(phy_get ${PHY})
199 if ! isset phy; then
200 log DEBUG "phy '${PHY}' is not present"
201 exit ${EXIT_ERROR}
202 fi
203
1ba6a2bb
MT
204 # Create the wireless device
205 wireless_create "${port}" \
206 --phy="${phy}" \
207 --type="ap" \
208 --address="${ADDRESS}"
d76f5107
MT
209
210 exit ${EXIT_OK}
211}
212
1c6a4e30 213hook_remove() {
1ba6a2bb 214 local port="${1}"
d76f5107
MT
215 assert isset port
216
b8026986
MT
217 # Remove the device if present
218 if device_exists "${port}"; then
219 wireless_remove "${port}"
47859d95 220 fi
d76f5107
MT
221
222 exit ${EXIT_OK}
223}
224
1c6a4e30 225hook_up() {
1ba6a2bb
MT
226 local port="${1}"
227 assert isset port
228
229 # The port must already exist before
230 # hostapd is started. Otherwise it will
231 # fail horribly over and over again.
232 assert device_exists "${port}"
233
234 hostapd_start "${port}"
235}
236
1c6a4e30 237hook_down() {
1ba6a2bb
MT
238 local port="${1}"
239 assert isset port
240
241 hostapd_stop "${port}"
242}
243
1c6a4e30 244hook_hotplug() {
b8026986 245 local port="${1}"
47859d95 246 assert isset port
49ec20d8 247
b8026986
MT
248 case "$(hotplug_action)" in
249 add)
1ba6a2bb
MT
250 # Create the port when the phy is plugged in
251 if hotplug_event_port_uses_phy "${port}"; then
252 hook_create "${port}"
b8026986
MT
253 fi
254 ;;
255
256 remove)
257 # Stop hostapd
258 if hotplug_event_port_is_interface "${port}"; then
259 hostapd_stop "${port}"
b8026986 260
1ba6a2bb
MT
261 exit ${EXIT_OK}
262 fi
b8026986
MT
263 ;;
264 esac
47859d95 265
1ba6a2bb 266 exit ${EXIT_NOT_HANDLED}
47859d95 267}