wireless-ap: Allow to disable DFS in 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 country_code="$(wireless_get_reg_domain)"
39         local dfs="on"
40         local encryption
41         local key
42         local mode
43         local ssid
44         local wmm="1"
45
46         while [ $# -gt 0 ]; do
47                 case "${1}" in
48                         --broadcast-ssid=*)
49                                 broadcast_ssid=$(cli_get_val "${1}")
50                                 ;;
51                         --channel=*)
52                                 channel=$(cli_get_val "${1}")
53                                 ;;
54                         --dfs=*)
55                                 dfs="$(cli_get_val "${1}")"
56                                 ;;
57                         --encryption=*)
58                                 encryption=$(cli_get_val "${1}")
59                                 ;;
60                         --key=*)
61                                 key=$(cli_get_val "${1}")
62                                 ;;
63                         --mode=*)
64                                 mode=$(cli_get_val "${1}")
65
66                                 if ! isoneof mode ${HOSTAPD_SUPPORTED_MODES}; then
67                                         error "Unsupported mode: ${mode}"
68                                         return ${EXIT_ERROR}
69                                 fi
70                                 ;;
71                         --ssid=*)
72                                 ssid=$(cli_get_val "${1}")
73                                 ;;
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                                 ;;
82                         *)
83                                 warning_log "Ignoring unknown argument '${1}'."
84                                 ;;                      
85                 esac
86                 shift
87         done
88
89         # Check if mode is set
90         if ! isset mode; then
91                 error "Mode is not set"
92                 return ${EXIT_ERROR}
93         fi
94
95         assert isset broadcast_ssid
96         assert isbool broadcast_ssid
97
98         assert isset channel
99         assert isinteger channel
100
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
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}")"
149
150                         # Fetch HT caps
151                         ht_caps="$(wireless_get_ht_caps "${device}")"
152                         ;;
153         esac
154
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                 print
167         ) >> ${file}
168
169         # Wireless configuration
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
177         (
178                 print "# Default settings"
179
180                 # Advertise country code and maximum transmission power
181                 print "ieee80211d=1"
182
183                 # Enable Radar Detection
184                 if enabled dfs; then
185                         print "ieee80211h=1"
186                 else
187                         print "ieee80211h=0"
188                 fi
189
190                 print # empty line
191
192                 print "# Wireless configuration"
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
203                 print "channel=${channel}"
204                 print "country_code=${country_code}"
205                 print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
206
207                 if contains_spaces "${ssid}"; then
208                         print "ssid=\"${ssid}\""
209                 else
210                         print "ssid=${ssid}"
211                 fi
212
213                 # WMM
214                 print "wmm_enabled=${wmm}"
215
216                 # Enable VHT caps
217                 if isset vht_caps; then
218                         print "vht_capab=${vht_caps}"
219                 fi
220
221                 # Enable HT caps
222                 print "ht_capab=${ht_caps}"
223
224                 print
225         ) >> ${file}
226
227         # Control interface.
228         (
229                 print "# Control interface"
230                 print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}"
231                 print "ctrl_interface_group=0"
232                 print
233         ) >> ${file}
234
235         # Encryption settings
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
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                         print
258                 ) >> ${file}
259         fi
260
261         return ${EXIT_OK}
262 }
263
264 hostapd_start() {
265         local device=${1}
266         assert isset device
267
268         service_start "hostapd@${device}.service"
269         local ret=$?
270
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}
279 }
280
281 hostapd_stop() {
282         local device=${1}
283         assert isset device
284
285         service_stop "hostapd@${device}.service"
286 }