]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.hostapd
hostapd: Apply channel bandwidth to configuration
[people/ms/network.git] / src / functions / functions.hostapd
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
22 HOSTAPD_CONTROL_INTERFACE_DIR="/run/hostapd/ctrl"
23
24 HOSTAPD_SUPPORTED_MODES="802.11a 802.11a/n 802.11ac 802.11g 802.11g/n"
25
26 hostapd_config_write() {
27 local device=${1}
28 assert isset device
29
30 local file=${2}
31 assert isset file
32
33 # Shift the device and file argument.
34 shift 2
35
36 local broadcast_ssid
37 local channel
38 local channel_bandwidth
39 local country_code="$(wireless_get_reg_domain)"
40 local dfs="on"
41 local encryption
42 local key
43 local mode
44 local ssid
45 local wmm="1"
46
47 while [ $# -gt 0 ]; do
48 case "${1}" in
49 --broadcast-ssid=*)
50 broadcast_ssid=$(cli_get_val "${1}")
51 ;;
52 --channel=*)
53 channel=$(cli_get_val "${1}")
54 ;;
55 --channel-bandwidth=*)
56 channel_bandwidth="$(cli_get_val "${1}")"
57 ;;
58 --dfs=*)
59 dfs="$(cli_get_val "${1}")"
60 ;;
61 --encryption=*)
62 encryption=$(cli_get_val "${1}")
63 ;;
64 --key=*)
65 key=$(cli_get_val "${1}")
66 ;;
67 --mode=*)
68 mode=$(cli_get_val "${1}")
69
70 if ! isoneof mode ${HOSTAPD_SUPPORTED_MODES}; then
71 error "Unsupported mode: ${mode}"
72 return ${EXIT_ERROR}
73 fi
74 ;;
75 --ssid=*)
76 ssid=$(cli_get_val "${1}")
77 ;;
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 ;;
86 *)
87 warning_log "Ignoring unknown argument '${1}'."
88 ;;
89 esac
90 shift
91 done
92
93 # Check if mode is set
94 if ! isset mode; then
95 error "Mode is not set"
96 return ${EXIT_ERROR}
97 fi
98
99 assert isset broadcast_ssid
100 assert isbool broadcast_ssid
101
102 assert isset channel
103 assert isinteger channel
104
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
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
120 # 802.11ac/n flags
121 local ieee80211ac
122 local ieee80211n
123 local vht_caps
124 local vht_oper_chwidth="0"
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}")"
160
161 # Fetch HT caps
162 ht_caps="$(wireless_get_ht_caps "${device}")"
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
175 ;;
176 esac
177
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 print
190 ) >> ${file}
191
192 # Wireless configuration
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
200 (
201 print "# Default settings"
202
203 # Advertise country code and maximum transmission power
204 print "ieee80211d=1"
205
206 # Enable Radar Detection
207 if enabled dfs && wireless_supports_dfs "${device}"; then
208 print "ieee80211h=1"
209 else
210 print "ieee80211h=0"
211 fi
212
213 print # empty line
214
215 print "# Wireless configuration"
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
226 print "channel=${channel}"
227 print "country_code=${country_code}"
228 print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
229
230 if contains_spaces "${ssid}"; then
231 print "ssid=\"${ssid}\""
232 else
233 print "ssid=${ssid}"
234 fi
235
236 # WMM
237 print "wmm_enabled=${wmm}"
238
239 # Enable VHT caps
240 if isset vht_caps; then
241 print "vht_capab=${vht_caps}"
242 fi
243
244 # Enable HT caps
245 print "ht_capab=${ht_caps}"
246
247 # Wider Channels
248 print "vht_oper_chwidth=${vht_oper_chwidth}"
249
250 print
251 ) >> ${file}
252
253 # Control interface.
254 (
255 print "# Control interface"
256 print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}"
257 print "ctrl_interface_group=0"
258 print
259 ) >> ${file}
260
261 # Encryption settings
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
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 print
284 ) >> ${file}
285 fi
286
287 return ${EXIT_OK}
288 }
289
290 hostapd_start() {
291 local device=${1}
292 assert isset device
293
294 service_start "hostapd@${device}.service"
295 local ret=$?
296
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}
305 }
306
307 hostapd_stop() {
308 local device=${1}
309 assert isset device
310
311 service_stop "hostapd@${device}.service"
312 }