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