]>
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 | 37 | local encryption |
4ed541a9 | 38 | local ieee80211d="1" |
0e035311 MT |
39 | local key |
40 | local mode | |
41 | local ssid | |
19c166f8 | 42 | local wmm="1" |
0e035311 MT |
43 | |
44 | while [ $# -gt 0 ]; do | |
45 | case "${1}" in | |
46 | --broadcast-ssid=*) | |
2212045f | 47 | broadcast_ssid=$(cli_get_val "${1}") |
0e035311 MT |
48 | ;; |
49 | --channel=*) | |
2212045f | 50 | channel=$(cli_get_val "${1}") |
0e035311 | 51 | ;; |
0e035311 | 52 | --encryption=*) |
2212045f | 53 | encryption=$(cli_get_val "${1}") |
0e035311 | 54 | ;; |
4ed541a9 MT |
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 | ;; | |
0e035311 | 63 | --key=*) |
2212045f | 64 | key=$(cli_get_val "${1}") |
0e035311 | 65 | ;; |
4cfc085f | 66 | --mode=*) |
2212045f | 67 | mode=$(cli_get_val "${1}") |
4cfc085f MT |
68 | ;; |
69 | --ssid=*) | |
2212045f | 70 | ssid=$(cli_get_val "${1}") |
4cfc085f | 71 | ;; |
19c166f8 MT |
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 | ;; | |
0e035311 MT |
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 | ||
0e035311 MT |
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 | ||
0e1c630c MT |
102 | # Get HT caps |
103 | local ht_caps="$(wireless_get_ht_caps "${device}")" | |
104 | ||
49ec20d8 MT |
105 | # Create configuration directory. |
106 | local config_dir=$(dirname ${file}) | |
107 | mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null | |
108 | ||
109 | config_header "hostapd" > ${file} | |
110 | ||
111 | # Interface configuration | |
112 | ( | |
113 | print "# Interface configuration" | |
114 | print "driver=nl80211" | |
115 | print "interface=${device}" | |
116 | ||
117 | ) >> ${file} | |
118 | ||
119 | # Wireless configuration | |
0e035311 MT |
120 | local ignore_broadcast_ssid |
121 | if enabled broadcast_ssid; then | |
122 | ignore_broadcast_ssid="0" | |
123 | else | |
124 | ignore_broadcast_ssid="1" | |
125 | fi | |
126 | ||
127 | local hw_mode ieee80211n="0" | |
128 | if [ "${mode}" = "n" ]; then | |
129 | if [ ${channel} -le 15 ]; then | |
130 | hw_mode="g" | |
131 | else | |
132 | hw_mode="a" | |
133 | fi | |
134 | ieee80211n="1" | |
ba5bb630 MT |
135 | else |
136 | hw_mode="${mode}" | |
0e035311 MT |
137 | fi |
138 | ||
49ec20d8 MT |
139 | ( |
140 | print "# Wireless configuration" | |
141 | print "channel=${channel}" | |
142 | print "country_code=${country_code}" | |
143 | print "hw_mode=${hw_mode}" | |
4ed541a9 | 144 | print "ieee80211d=${ieee80211d}" |
49ec20d8 MT |
145 | print "ieee80211n=${ieee80211n}" |
146 | print "ignore_broadcast_ssid=${ignore_broadcast_ssid}" | |
0e035311 | 147 | |
49ec20d8 MT |
148 | if contains_spaces "${ssid}"; then |
149 | print "ssid=\"${ssid}\"" | |
150 | else | |
151 | print "ssid=${ssid}" | |
152 | fi | |
0e035311 | 153 | |
19c166f8 MT |
154 | # WMM |
155 | print "wmm_enabled=${wmm}" | |
156 | ||
0e1c630c MT |
157 | # Enable HT caps |
158 | print "ht_capab=${ht_caps}" | |
159 | ||
49ec20d8 MT |
160 | |
161 | ) >> ${file} | |
0e035311 | 162 | |
49ec20d8 MT |
163 | # Control interface. |
164 | ( | |
165 | print "# Control interface" | |
166 | print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}" | |
167 | print "ctrl_interface_group=0" | |
168 | ||
169 | ) >> ${file} | |
0e035311 | 170 | |
49ec20d8 | 171 | # Encryption settings |
0e035311 MT |
172 | if isset encryption; then |
173 | local encryption_mode=0 | |
174 | case "${encryption}" in | |
175 | WPA) | |
176 | encryption_mode=1 | |
177 | ;; | |
178 | WPA2) | |
179 | encryption_mode=2 | |
180 | ;; | |
181 | WPA/WPA2) | |
182 | encryption_mode=3 | |
183 | ;; | |
184 | esac | |
185 | ||
49ec20d8 MT |
186 | ( |
187 | print "# Encryption settings" | |
188 | print "wpa=${encryption_mode}" | |
189 | print "wpa_passphrase=${key}" | |
190 | print "wpa_key_mgmt=WPA-PSK" | |
191 | print "wpa_pairwise=TKIP" | |
192 | print "rsn_pairwise=CCMP" | |
193 | ||
194 | ) >> ${file} | |
0e035311 MT |
195 | fi |
196 | ||
197 | return ${EXIT_OK} | |
198 | } | |
199 | ||
1c6a4e30 | 200 | hostapd_start() { |
0e035311 | 201 | local device=${1} |
0e035311 MT |
202 | assert isset device |
203 | ||
0e035311 MT |
204 | service_start "hostapd@${device}.service" |
205 | local ret=$? | |
206 | ||
49ec20d8 MT |
207 | if [ ${ret} -eq ${EXIT_OK} ]; then |
208 | log DEBUG "hostapd has been successfully started on '${device}'" | |
209 | else | |
210 | log ERROR "Could not start hostapd on '${device}': ${ret}" | |
211 | return ${EXIT_ERROR} | |
212 | fi | |
213 | ||
214 | return ${EXIT_OK} | |
0e035311 MT |
215 | } |
216 | ||
1c6a4e30 | 217 | hostapd_stop() { |
0e035311 MT |
218 | local device=${1} |
219 | assert isset device | |
220 | ||
221 | service_stop "hostapd@${device}.service" | |
0e035311 | 222 | } |