wireless-ap: Allow setting the wireless environment (indoor/outdoor)
[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 environment="${WIRELESS_DEFAULT_ENVIRONMENT}"
43         local key
44         local mode
45         local ssid
46         local wmm="1"
47
48         while [ $# -gt 0 ]; do
49                 case "${1}" in
50                         --broadcast-ssid=*)
51                                 broadcast_ssid=$(cli_get_val "${1}")
52                                 ;;
53                         --channel=*)
54                                 channel=$(cli_get_val "${1}")
55                                 ;;
56                         --channel-bandwidth=*)
57                                 channel_bandwidth="$(cli_get_val "${1}")"
58                                 ;;
59                         --dfs=*)
60                                 dfs="$(cli_get_val "${1}")"
61                                 ;;
62                         --encryption=*)
63                                 encryption=$(cli_get_val "${1}")
64                                 ;;
65                         --environment=*)
66                                 environment="$(cli_get_val "${1}")"
67                                 ;;
68                         --key=*)
69                                 key=$(cli_get_val "${1}")
70                                 ;;
71                         --mode=*)
72                                 mode=$(cli_get_val "${1}")
73
74                                 if ! isoneof mode ${HOSTAPD_SUPPORTED_MODES}; then
75                                         error "Unsupported mode: ${mode}"
76                                         return ${EXIT_ERROR}
77                                 fi
78                                 ;;
79                         --ssid=*)
80                                 ssid=$(cli_get_val "${1}")
81                                 ;;
82                         --wmm=*)
83                                 local val="$(cli_get_val "${1}")"
84                                 if enabled val; then
85                                         wmm="1"
86                                 else
87                                         wmm="0"
88                                 fi
89                                 ;;
90                         *)
91                                 warning_log "Ignoring unknown argument '${1}'."
92                                 ;;                      
93                 esac
94                 shift
95         done
96
97         # Check if mode is set
98         if ! isset mode; then
99                 error "Mode is not set"
100                 return ${EXIT_ERROR}
101         fi
102
103         assert isset broadcast_ssid
104         assert isbool broadcast_ssid
105
106         assert isset channel
107         assert isinteger channel
108
109         assert isset mode
110         assert isset ssid
111
112         # Check if key is set when encryption is used.
113         if isset encryption; then
114                 assert isoneof encryption WPA WPA2 WPA/WPA2
115                 assert isset key
116         fi
117
118         # Check wireless environment
119         if ! wireless_environment_is_valid "${environment}"; then
120                 error "Invalid wireless environment: ${environment}"
121                 return ${EXIT_ERROR}
122         fi
123
124         # With channel 0, ACS must be supported
125         if [ ${channel} -eq 0 ] && ! wireless_supports_acs "${device}"; then
126                 error "ACS requested, but not supported by ${device}"
127                 return ${EXIT_ERROR}
128         fi
129
130         # Check channel bandwidth for validity
131         if isset channel_bandwidth && ! wireless_channel_bandwidth_is_valid "${mode}" "${channel_bandwidth}"; then
132                 error "Invalid channel bandwidth for ${mode}: ${channel_bandwidth}"
133                 return ${EXIT_ERROR}
134         fi
135
136         # 802.11ac/n flags
137         local ieee80211ac
138         local ieee80211n
139         local vht_caps
140         local vht_oper_chwidth="0"
141         local ht_caps
142
143         local hw_mode
144         case "${mode}" in
145                 802.11a)
146                         hw_mode="a"
147                         ;;
148
149                 802.11a/n)
150                         hw_mode="a"
151                         ieee80211n="1"
152
153                         # Fetch HT caps
154                         ht_caps="$(wireless_get_ht_caps "${device}")"
155                         ;;
156
157                 802.11g)
158                         hw_mode="g"
159                         ;;
160
161                 802.11g/n)
162                         hw_mode="g"
163                         ieee80211n="1"
164
165                         # Fetch HT caps
166                         ht_caps="$(wireless_get_ht_caps "${device}")"
167                         ;;
168
169                 802.11ac)
170                         hw_mode="a"
171                         ieee80211ac="1"
172                         ieee80211n="1"
173
174                         # Fetch VHT caps
175                         vht_caps="$(wireless_get_vht_caps "${device}")"
176
177                         # Fetch HT caps
178                         ht_caps="$(wireless_get_ht_caps "${device}")"
179
180                         case "${channel_bandwidth}" in
181                                 80)
182                                         vht_oper_chwidth="1"
183                                         ;;
184                                 160)
185                                         vht_oper_chwidth="2"
186                                         ;;
187                                 80+80)
188                                         vht_oper_chwidth="3"
189                                         ;;
190                         esac
191                         ;;
192         esac
193
194         # Create configuration directory.
195         local config_dir=$(dirname ${file})
196         mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null
197
198         config_header "hostapd" > ${file}
199
200         # Interface configuration
201         (
202                 print "# Interface configuration"
203                 print "driver=nl80211"
204                 print "interface=${device}"
205                 print
206         ) >> ${file}
207
208         # Wireless configuration
209         local ignore_broadcast_ssid
210         if enabled broadcast_ssid; then
211                 ignore_broadcast_ssid="0"
212         else
213                 ignore_broadcast_ssid="1"
214         fi
215
216         (
217                 print "# Default settings"
218
219                 # Advertise country code and maximum transmission power
220                 print "ieee80211d=1"
221                 print "country_code=${country_code}"
222
223                 # Wireless Environment
224                 case "${environment}" in
225                         indoor)
226                                 print "country3=0x49"
227                                        country3
228                                 ;;
229                         outdoor)
230                                 print "country3=0x4f"
231                                 ;;
232                         indoor+outdoor)
233                                 print "country3=0x20"
234                                 ;;
235                 esac
236
237                 # Enable Radar Detection
238                 if enabled dfs && wireless_supports_dfs "${device}"; then
239                         print "ieee80211h=1"
240                 else
241                         print "ieee80211h=0"
242                 fi
243
244                 print # empty line
245
246                 print "# Wireless configuration"
247                 print "hw_mode=${hw_mode}"
248
249                 if isset ieee80211ac; then
250                         print "ieee80211ac=${ieee80211ac}"
251                 fi
252
253                 if isset ieee80211n; then
254                         print "ieee80211n=${ieee80211n}"
255                 fi
256
257                 print "channel=${channel}"
258                 print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
259
260                 if contains_spaces "${ssid}"; then
261                         print "ssid=\"${ssid}\""
262                 else
263                         print "ssid=${ssid}"
264                 fi
265
266                 # WMM
267                 print "wmm_enabled=${wmm}"
268
269                 # Enable VHT caps
270                 if isset vht_caps; then
271                         print "vht_capab=${vht_caps}"
272                 fi
273
274                 # Enable HT caps
275                 print "ht_capab=${ht_caps}"
276
277                 # Wider Channels
278                 print "vht_oper_chwidth=${vht_oper_chwidth}"
279
280                 print
281         ) >> ${file}
282
283         # Control interface.
284         (
285                 print "# Control interface"
286                 print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}"
287                 print "ctrl_interface_group=0"
288                 print
289         ) >> ${file}
290
291         # Encryption settings
292         if isset encryption; then
293                 local encryption_mode=0
294                 case "${encryption}" in
295                         WPA)
296                                 encryption_mode=1
297                                 ;;
298                         WPA2)
299                                 encryption_mode=2
300                                 ;;
301                         WPA/WPA2)
302                                 encryption_mode=3
303                                 ;;
304                 esac
305
306                 (
307                         print "# Encryption settings"
308                         print "wpa=${encryption_mode}"
309                         print "wpa_passphrase=${key}"
310                         print "wpa_key_mgmt=WPA-PSK"
311                         print "wpa_pairwise=TKIP"
312                         print "rsn_pairwise=CCMP"
313                         print
314                 ) >> ${file}
315         fi
316
317         return ${EXIT_OK}
318 }
319
320 hostapd_start() {
321         local device=${1}
322         assert isset device
323
324         service_start "hostapd@${device}.service"
325         local ret=$?
326
327         if [ ${ret} -eq ${EXIT_OK} ]; then
328                 log DEBUG "hostapd has been successfully started on '${device}'"
329         else
330                 log ERROR "Could not start hostapd on '${device}': ${ret}"
331                 return ${EXIT_ERROR}
332         fi
333
334         return ${EXIT_OK}
335 }
336
337 hostapd_stop() {
338         local device=${1}
339         assert isset device
340
341         service_stop "hostapd@${device}.service"
342 }