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