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