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