3f64e79b9f24cc0463ba1c551c73694d0471a705
[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 encryption
40         local key
41         local mode
42         local ssid
43         local wmm="1"
44
45         while [ $# -gt 0 ]; do
46                 case "${1}" in
47                         --broadcast-ssid=*)
48                                 broadcast_ssid=$(cli_get_val "${1}")
49                                 ;;
50                         --channel=*)
51                                 channel=$(cli_get_val "${1}")
52                                 ;;
53                         --encryption=*)
54                                 encryption=$(cli_get_val "${1}")
55                                 ;;
56                         --key=*)
57                                 key=$(cli_get_val "${1}")
58                                 ;;
59                         --mode=*)
60                                 mode=$(cli_get_val "${1}")
61
62                                 if ! isoneof mode ${HOSTAPD_SUPPORTED_MODES}; then
63                                         error "Unsupported mode: ${mode}"
64                                         return ${EXIT_ERROR}
65                                 fi
66                                 ;;
67                         --ssid=*)
68                                 ssid=$(cli_get_val "${1}")
69                                 ;;
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                                 ;;
78                         *)
79                                 warning_log "Ignoring unknown argument '${1}'."
80                                 ;;                      
81                 esac
82                 shift
83         done
84
85         # Check if mode is set
86         if ! isset mode; then
87                 error "Mode is not set"
88                 return ${EXIT_ERROR}
89         fi
90
91         assert isset broadcast_ssid
92         assert isbool broadcast_ssid
93
94         assert isset channel
95         assert isinteger channel
96
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
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}")"
145
146                         # Fetch HT caps
147                         ht_caps="$(wireless_get_ht_caps "${device}")"
148                         ;;
149         esac
150
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                 print
163         ) >> ${file}
164
165         # Wireless configuration
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
173         (
174                 print "# Default settings"
175
176                 # Advertise country code and maximum transmission power
177                 print "ieee80211d=1"
178
179                 # Enable Radar Detection
180                 print "ieee80211h=1"
181
182                 print # empty line
183
184                 print "# Wireless configuration"
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
195                 print "channel=${channel}"
196                 print "country_code=${country_code}"
197                 print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
198
199                 if contains_spaces "${ssid}"; then
200                         print "ssid=\"${ssid}\""
201                 else
202                         print "ssid=${ssid}"
203                 fi
204
205                 # WMM
206                 print "wmm_enabled=${wmm}"
207
208                 # Enable VHT caps
209                 if isset vht_caps; then
210                         print "vht_capab=${vht_caps}"
211                 fi
212
213                 # Enable HT caps
214                 print "ht_capab=${ht_caps}"
215
216                 print
217         ) >> ${file}
218
219         # Control interface.
220         (
221                 print "# Control interface"
222                 print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}"
223                 print "ctrl_interface_group=0"
224                 print
225         ) >> ${file}
226
227         # Encryption settings
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
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                         print
250                 ) >> ${file}
251         fi
252
253         return ${EXIT_OK}
254 }
255
256 hostapd_start() {
257         local device=${1}
258         assert isset device
259
260         service_start "hostapd@${device}.service"
261         local ret=$?
262
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}
271 }
272
273 hostapd_stop() {
274         local device=${1}
275         assert isset device
276
277         service_stop "hostapd@${device}.service"
278 }