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