]>
Commit | Line | Data |
---|---|---|
0e035311 MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2012 IPFire Network Development Team # | |
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 | ||
49ec20d8 | 22 | HOSTAPD_CONTROL_INTERFACE_DIR="/run/hostapd/ctrl" |
0e035311 | 23 | |
6c262922 MT |
24 | HOSTAPD_SUPPORTED_MODES="802.11a 802.11a/n 802.11ac 802.11g 802.11g/n" |
25 | ||
1c6a4e30 | 26 | hostapd_config_write() { |
0e035311 | 27 | local device=${1} |
49ec20d8 | 28 | assert isset device |
0e035311 | 29 | |
49ec20d8 MT |
30 | local file=${2} |
31 | assert isset file | |
32 | ||
33 | # Shift the device and file argument. | |
34 | shift 2 | |
0e035311 MT |
35 | |
36 | local broadcast_ssid | |
37 | local channel | |
f9e980d9 | 38 | local channel_bandwidth |
31670741 | 39 | local country_code="$(wireless_get_reg_domain)" |
7b297fb2 | 40 | local dfs="on" |
0e035311 MT |
41 | local encryption |
42 | local key | |
43 | local mode | |
44 | local ssid | |
19c166f8 | 45 | local wmm="1" |
0e035311 MT |
46 | |
47 | while [ $# -gt 0 ]; do | |
48 | case "${1}" in | |
49 | --broadcast-ssid=*) | |
2212045f | 50 | broadcast_ssid=$(cli_get_val "${1}") |
0e035311 MT |
51 | ;; |
52 | --channel=*) | |
2212045f | 53 | channel=$(cli_get_val "${1}") |
0e035311 | 54 | ;; |
f9e980d9 MT |
55 | --channel-bandwidth=*) |
56 | channel_bandwidth="$(cli_get_val "${1}")" | |
57 | ;; | |
7b297fb2 MT |
58 | --dfs=*) |
59 | dfs="$(cli_get_val "${1}")" | |
60 | ;; | |
0e035311 | 61 | --encryption=*) |
2212045f | 62 | encryption=$(cli_get_val "${1}") |
0e035311 MT |
63 | ;; |
64 | --key=*) | |
2212045f | 65 | key=$(cli_get_val "${1}") |
0e035311 | 66 | ;; |
4cfc085f | 67 | --mode=*) |
2212045f | 68 | mode=$(cli_get_val "${1}") |
6c262922 MT |
69 | |
70 | if ! isoneof mode ${HOSTAPD_SUPPORTED_MODES}; then | |
71 | error "Unsupported mode: ${mode}" | |
72 | return ${EXIT_ERROR} | |
73 | fi | |
4cfc085f MT |
74 | ;; |
75 | --ssid=*) | |
2212045f | 76 | ssid=$(cli_get_val "${1}") |
4cfc085f | 77 | ;; |
19c166f8 MT |
78 | --wmm=*) |
79 | local val="$(cli_get_val "${1}")" | |
80 | if enabled val; then | |
81 | wmm="1" | |
82 | else | |
83 | wmm="0" | |
84 | fi | |
85 | ;; | |
0e035311 MT |
86 | *) |
87 | warning_log "Ignoring unknown argument '${1}'." | |
88 | ;; | |
89 | esac | |
90 | shift | |
91 | done | |
92 | ||
6c262922 MT |
93 | # Check if mode is set |
94 | if ! isset mode; then | |
95 | error "Mode is not set" | |
96 | return ${EXIT_ERROR} | |
97 | fi | |
98 | ||
0e035311 MT |
99 | assert isset broadcast_ssid |
100 | assert isbool broadcast_ssid | |
101 | ||
102 | assert isset channel | |
103 | assert isinteger channel | |
104 | ||
0e035311 MT |
105 | assert isset mode |
106 | assert isset ssid | |
107 | ||
108 | # Check if key is set when encryption is used. | |
109 | if isset encryption; then | |
110 | assert isoneof encryption WPA WPA2 WPA/WPA2 | |
111 | assert isset key | |
112 | fi | |
113 | ||
f9e980d9 MT |
114 | # Check channel bandwidth for validity |
115 | if isset channel_bandwidth && ! wireless_channel_bandwidth_is_valid "${mode}" "${channel_bandwidth}"; then | |
116 | error "Invalid channel bandwidth for ${mode}: ${channel_bandwidth}" | |
117 | return ${EXIT_ERROR} | |
118 | fi | |
119 | ||
6c262922 MT |
120 | # 802.11ac/n flags |
121 | local ieee80211ac | |
122 | local ieee80211n | |
123 | local vht_caps | |
f9e980d9 | 124 | local vht_oper_chwidth="0" |
6c262922 MT |
125 | local ht_caps |
126 | ||
127 | local hw_mode | |
128 | case "${mode}" in | |
129 | 802.11a) | |
130 | hw_mode="a" | |
131 | ;; | |
132 | ||
133 | 802.11a/n) | |
134 | hw_mode="a" | |
135 | ieee80211n="1" | |
136 | ||
137 | # Fetch HT caps | |
138 | ht_caps="$(wireless_get_ht_caps "${device}")" | |
139 | ;; | |
140 | ||
141 | 802.11g) | |
142 | hw_mode="g" | |
143 | ;; | |
144 | ||
145 | 802.11g/n) | |
146 | hw_mode="g" | |
147 | ieee80211n="1" | |
148 | ||
149 | # Fetch HT caps | |
150 | ht_caps="$(wireless_get_ht_caps "${device}")" | |
151 | ;; | |
152 | ||
153 | 802.11ac) | |
154 | hw_mode="a" | |
155 | ieee80211ac="1" | |
156 | ieee80211n="1" | |
157 | ||
158 | # Fetch VHT caps | |
159 | vht_caps="$(wireless_get_vht_caps "${device}")" | |
1526e219 | 160 | |
6c262922 MT |
161 | # Fetch HT caps |
162 | ht_caps="$(wireless_get_ht_caps "${device}")" | |
f9e980d9 MT |
163 | |
164 | case "${channel_bandwidth}" in | |
165 | 80) | |
166 | vht_oper_chwidth="1" | |
167 | ;; | |
168 | 160) | |
169 | vht_oper_chwidth="2" | |
170 | ;; | |
171 | 80+80) | |
172 | vht_oper_chwidth="3" | |
173 | ;; | |
174 | esac | |
6c262922 MT |
175 | ;; |
176 | esac | |
0e1c630c | 177 | |
49ec20d8 MT |
178 | # Create configuration directory. |
179 | local config_dir=$(dirname ${file}) | |
180 | mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null | |
181 | ||
182 | config_header "hostapd" > ${file} | |
183 | ||
184 | # Interface configuration | |
185 | ( | |
186 | print "# Interface configuration" | |
187 | print "driver=nl80211" | |
188 | print "interface=${device}" | |
189 | ||
190 | ) >> ${file} | |
191 | ||
192 | # Wireless configuration | |
0e035311 MT |
193 | local ignore_broadcast_ssid |
194 | if enabled broadcast_ssid; then | |
195 | ignore_broadcast_ssid="0" | |
196 | else | |
197 | ignore_broadcast_ssid="1" | |
198 | fi | |
199 | ||
49ec20d8 | 200 | ( |
b6ec3dd6 MT |
201 | print "# Default settings" |
202 | ||
203 | # Advertise country code and maximum transmission power | |
204 | print "ieee80211d=1" | |
205 | ||
6c262922 | 206 | # Enable Radar Detection |
dc6d97fb | 207 | if enabled dfs && wireless_supports_dfs "${device}"; then |
7b297fb2 MT |
208 | print "ieee80211h=1" |
209 | else | |
210 | print "ieee80211h=0" | |
211 | fi | |
6c262922 MT |
212 | |
213 | print # empty line | |
214 | ||
49ec20d8 | 215 | print "# Wireless configuration" |
6c262922 MT |
216 | print "hw_mode=${hw_mode}" |
217 | ||
218 | if isset ieee80211ac; then | |
219 | print "ieee80211ac=${ieee80211ac}" | |
220 | fi | |
221 | ||
222 | if isset ieee80211n; then | |
223 | print "ieee80211n=${ieee80211n}" | |
224 | fi | |
225 | ||
49ec20d8 MT |
226 | print "channel=${channel}" |
227 | print "country_code=${country_code}" | |
49ec20d8 | 228 | print "ignore_broadcast_ssid=${ignore_broadcast_ssid}" |
0e035311 | 229 | |
49ec20d8 MT |
230 | if contains_spaces "${ssid}"; then |
231 | print "ssid=\"${ssid}\"" | |
232 | else | |
233 | print "ssid=${ssid}" | |
234 | fi | |
0e035311 | 235 | |
19c166f8 MT |
236 | # WMM |
237 | print "wmm_enabled=${wmm}" | |
238 | ||
1526e219 MT |
239 | # Enable VHT caps |
240 | if isset vht_caps; then | |
241 | print "vht_capab=${vht_caps}" | |
242 | fi | |
243 | ||
0e1c630c MT |
244 | # Enable HT caps |
245 | print "ht_capab=${ht_caps}" | |
246 | ||
f9e980d9 MT |
247 | # Wider Channels |
248 | print "vht_oper_chwidth=${vht_oper_chwidth}" | |
249 | ||
49ec20d8 MT |
250 | |
251 | ) >> ${file} | |
0e035311 | 252 | |
49ec20d8 MT |
253 | # Control interface. |
254 | ( | |
255 | print "# Control interface" | |
256 | print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}" | |
257 | print "ctrl_interface_group=0" | |
258 | ||
259 | ) >> ${file} | |
0e035311 | 260 | |
49ec20d8 | 261 | # Encryption settings |
0e035311 MT |
262 | if isset encryption; then |
263 | local encryption_mode=0 | |
264 | case "${encryption}" in | |
265 | WPA) | |
266 | encryption_mode=1 | |
267 | ;; | |
268 | WPA2) | |
269 | encryption_mode=2 | |
270 | ;; | |
271 | WPA/WPA2) | |
272 | encryption_mode=3 | |
273 | ;; | |
274 | esac | |
275 | ||
49ec20d8 MT |
276 | ( |
277 | print "# Encryption settings" | |
278 | print "wpa=${encryption_mode}" | |
279 | print "wpa_passphrase=${key}" | |
280 | print "wpa_key_mgmt=WPA-PSK" | |
281 | print "wpa_pairwise=TKIP" | |
282 | print "rsn_pairwise=CCMP" | |
283 | ||
284 | ) >> ${file} | |
0e035311 MT |
285 | fi |
286 | ||
287 | return ${EXIT_OK} | |
288 | } | |
289 | ||
1c6a4e30 | 290 | hostapd_start() { |
0e035311 | 291 | local device=${1} |
0e035311 MT |
292 | assert isset device |
293 | ||
0e035311 MT |
294 | service_start "hostapd@${device}.service" |
295 | local ret=$? | |
296 | ||
49ec20d8 MT |
297 | if [ ${ret} -eq ${EXIT_OK} ]; then |
298 | log DEBUG "hostapd has been successfully started on '${device}'" | |
299 | else | |
300 | log ERROR "Could not start hostapd on '${device}': ${ret}" | |
301 | return ${EXIT_ERROR} | |
302 | fi | |
303 | ||
304 | return ${EXIT_OK} | |
0e035311 MT |
305 | } |
306 | ||
1c6a4e30 | 307 | hostapd_stop() { |
0e035311 MT |
308 | local device=${1} |
309 | assert isset device | |
310 | ||
311 | service_stop "hostapd@${device}.service" | |
0e035311 | 312 | } |