]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.hostapd
hostapd: Write VHT capabilities 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_config_write() {
25 local device=${1}
26 assert isset device
27
28 local file=${2}
29 assert isset file
30
31 # Shift the device and file argument.
32 shift 2
33
34 local broadcast_ssid
35 local channel
36 local country_code="$(wireless_get_reg_domain)"
37 local encryption
38 local ieee80211d="1"
39 local key
40 local mode
41 local ssid
42 local wmm="1"
43
44 while [ $# -gt 0 ]; do
45 case "${1}" in
46 --broadcast-ssid=*)
47 broadcast_ssid=$(cli_get_val "${1}")
48 ;;
49 --channel=*)
50 channel=$(cli_get_val "${1}")
51 ;;
52 --encryption=*)
53 encryption=$(cli_get_val "${1}")
54 ;;
55 --ieee80211d=*)
56 local val="$(cli_get_val "${1}")"
57 if enabled val; then
58 ieee80211d="1"
59 else
60 ieee80211d="0"
61 fi
62 ;;
63 --key=*)
64 key=$(cli_get_val "${1}")
65 ;;
66 --mode=*)
67 mode=$(cli_get_val "${1}")
68 ;;
69 --ssid=*)
70 ssid=$(cli_get_val "${1}")
71 ;;
72 --wmm=*)
73 local val="$(cli_get_val "${1}")"
74 if enabled val; then
75 wmm="1"
76 else
77 wmm="0"
78 fi
79 ;;
80 *)
81 warning_log "Ignoring unknown argument '${1}'."
82 ;;
83 esac
84 shift
85 done
86
87 assert isset broadcast_ssid
88 assert isbool broadcast_ssid
89
90 assert isset channel
91 assert isinteger channel
92
93 assert isset mode
94 assert isset ssid
95
96 # Check if key is set when encryption is used.
97 if isset encryption; then
98 assert isoneof encryption WPA WPA2 WPA/WPA2
99 assert isset key
100 fi
101
102 # Get VHT caps
103 local vht_caps="$(wireless_get_vht_caps "${device}")"
104
105 # Get HT caps
106 local ht_caps="$(wireless_get_ht_caps "${device}")"
107
108 # Create configuration directory.
109 local config_dir=$(dirname ${file})
110 mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null
111
112 config_header "hostapd" > ${file}
113
114 # Interface configuration
115 (
116 print "# Interface configuration"
117 print "driver=nl80211"
118 print "interface=${device}"
119 print
120 ) >> ${file}
121
122 # Wireless configuration
123 local ignore_broadcast_ssid
124 if enabled broadcast_ssid; then
125 ignore_broadcast_ssid="0"
126 else
127 ignore_broadcast_ssid="1"
128 fi
129
130 local hw_mode ieee80211n="0"
131 if [ "${mode}" = "n" ]; then
132 if [ ${channel} -le 15 ]; then
133 hw_mode="g"
134 else
135 hw_mode="a"
136 fi
137 ieee80211n="1"
138 else
139 hw_mode="${mode}"
140 fi
141
142 (
143 print "# Wireless configuration"
144 print "channel=${channel}"
145 print "country_code=${country_code}"
146 print "hw_mode=${hw_mode}"
147 print "ieee80211d=${ieee80211d}"
148 print "ieee80211n=${ieee80211n}"
149 print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
150
151 if contains_spaces "${ssid}"; then
152 print "ssid=\"${ssid}\""
153 else
154 print "ssid=${ssid}"
155 fi
156
157 # WMM
158 print "wmm_enabled=${wmm}"
159
160 # Enable VHT caps
161 if isset vht_caps; then
162 print "vht_capab=${vht_caps}"
163 fi
164
165 # Enable HT caps
166 print "ht_capab=${ht_caps}"
167
168 print
169 ) >> ${file}
170
171 # Control interface.
172 (
173 print "# Control interface"
174 print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}"
175 print "ctrl_interface_group=0"
176 print
177 ) >> ${file}
178
179 # Encryption settings
180 if isset encryption; then
181 local encryption_mode=0
182 case "${encryption}" in
183 WPA)
184 encryption_mode=1
185 ;;
186 WPA2)
187 encryption_mode=2
188 ;;
189 WPA/WPA2)
190 encryption_mode=3
191 ;;
192 esac
193
194 (
195 print "# Encryption settings"
196 print "wpa=${encryption_mode}"
197 print "wpa_passphrase=${key}"
198 print "wpa_key_mgmt=WPA-PSK"
199 print "wpa_pairwise=TKIP"
200 print "rsn_pairwise=CCMP"
201 print
202 ) >> ${file}
203 fi
204
205 return ${EXIT_OK}
206 }
207
208 hostapd_start() {
209 local device=${1}
210 assert isset device
211
212 service_start "hostapd@${device}.service"
213 local ret=$?
214
215 if [ ${ret} -eq ${EXIT_OK} ]; then
216 log DEBUG "hostapd has been successfully started on '${device}'"
217 else
218 log ERROR "Could not start hostapd on '${device}': ${ret}"
219 return ${EXIT_ERROR}
220 fi
221
222 return ${EXIT_OK}
223 }
224
225 hostapd_stop() {
226 local device=${1}
227 assert isset device
228
229 service_stop "hostapd@${device}.service"
230 }