]>
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 MT |
23 | |
24 | function hostapd_config_write() { | |
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 | |
36 | local country_code | |
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=*) | |
49ec20d8 | 47 | broadcast_ssid=$(cli_get_val ${1}) |
0e035311 MT |
48 | ;; |
49 | --channel=*) | |
49ec20d8 | 50 | channel=$(cli_get_val ${1}) |
0e035311 MT |
51 | ;; |
52 | --country-code=*) | |
49ec20d8 | 53 | country_code=$(cli_get_val ${1}) |
0e035311 | 54 | ;; |
0e035311 MT |
55 | --encryption=*) |
56 | encryption=$(cli_get_val ${1}) | |
57 | ;; | |
4ed541a9 MT |
58 | --ieee80211d=*) |
59 | local val="$(cli_get_val "${1}")" | |
60 | if enabled val; then | |
61 | ieee80211d="1" | |
62 | else | |
63 | ieee80211d="0" | |
64 | fi | |
65 | ;; | |
0e035311 MT |
66 | --key=*) |
67 | key=$(cli_get_val ${1}) | |
68 | ;; | |
4cfc085f MT |
69 | --mode=*) |
70 | mode=$(cli_get_val ${1}) | |
71 | ;; | |
72 | --ssid=*) | |
73 | ssid=$(cli_get_val ${1}) | |
74 | ;; | |
19c166f8 MT |
75 | --wmm=*) |
76 | local val="$(cli_get_val "${1}")" | |
77 | if enabled val; then | |
78 | wmm="1" | |
79 | else | |
80 | wmm="0" | |
81 | fi | |
82 | ;; | |
0e035311 MT |
83 | *) |
84 | warning_log "Ignoring unknown argument '${1}'." | |
85 | ;; | |
86 | esac | |
87 | shift | |
88 | done | |
89 | ||
90 | assert isset broadcast_ssid | |
91 | assert isbool broadcast_ssid | |
92 | ||
93 | assert isset channel | |
94 | assert isinteger channel | |
95 | ||
96 | assert isset country_code | |
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 | ||
49ec20d8 MT |
106 | # Create configuration directory. |
107 | local config_dir=$(dirname ${file}) | |
108 | mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null | |
109 | ||
110 | config_header "hostapd" > ${file} | |
111 | ||
112 | # Interface configuration | |
113 | ( | |
114 | print "# Interface configuration" | |
115 | print "driver=nl80211" | |
116 | print "interface=${device}" | |
117 | ||
118 | ) >> ${file} | |
119 | ||
120 | # Wireless configuration | |
0e035311 MT |
121 | local ignore_broadcast_ssid |
122 | if enabled broadcast_ssid; then | |
123 | ignore_broadcast_ssid="0" | |
124 | else | |
125 | ignore_broadcast_ssid="1" | |
126 | fi | |
127 | ||
128 | local hw_mode ieee80211n="0" | |
129 | if [ "${mode}" = "n" ]; then | |
130 | if [ ${channel} -le 15 ]; then | |
131 | hw_mode="g" | |
132 | else | |
133 | hw_mode="a" | |
134 | fi | |
135 | ieee80211n="1" | |
ba5bb630 MT |
136 | else |
137 | hw_mode="${mode}" | |
0e035311 MT |
138 | fi |
139 | ||
49ec20d8 MT |
140 | ( |
141 | print "# Wireless configuration" | |
142 | print "channel=${channel}" | |
143 | print "country_code=${country_code}" | |
144 | print "hw_mode=${hw_mode}" | |
4ed541a9 | 145 | print "ieee80211d=${ieee80211d}" |
49ec20d8 MT |
146 | print "ieee80211n=${ieee80211n}" |
147 | print "ignore_broadcast_ssid=${ignore_broadcast_ssid}" | |
0e035311 | 148 | |
49ec20d8 MT |
149 | if contains_spaces "${ssid}"; then |
150 | print "ssid=\"${ssid}\"" | |
151 | else | |
152 | print "ssid=${ssid}" | |
153 | fi | |
0e035311 | 154 | |
19c166f8 MT |
155 | # WMM |
156 | print "wmm_enabled=${wmm}" | |
157 | ||
49ec20d8 MT |
158 | |
159 | ) >> ${file} | |
0e035311 | 160 | |
49ec20d8 MT |
161 | # Control interface. |
162 | ( | |
163 | print "# Control interface" | |
164 | print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}" | |
165 | print "ctrl_interface_group=0" | |
166 | ||
167 | ) >> ${file} | |
0e035311 | 168 | |
49ec20d8 | 169 | # Encryption settings |
0e035311 MT |
170 | if isset encryption; then |
171 | local encryption_mode=0 | |
172 | case "${encryption}" in | |
173 | WPA) | |
174 | encryption_mode=1 | |
175 | ;; | |
176 | WPA2) | |
177 | encryption_mode=2 | |
178 | ;; | |
179 | WPA/WPA2) | |
180 | encryption_mode=3 | |
181 | ;; | |
182 | esac | |
183 | ||
49ec20d8 MT |
184 | ( |
185 | print "# Encryption settings" | |
186 | print "wpa=${encryption_mode}" | |
187 | print "wpa_passphrase=${key}" | |
188 | print "wpa_key_mgmt=WPA-PSK" | |
189 | print "wpa_pairwise=TKIP" | |
190 | print "rsn_pairwise=CCMP" | |
191 | ||
192 | ) >> ${file} | |
0e035311 MT |
193 | fi |
194 | ||
195 | return ${EXIT_OK} | |
196 | } | |
197 | ||
198 | function hostapd_start() { | |
199 | local device=${1} | |
0e035311 MT |
200 | assert isset device |
201 | ||
0e035311 MT |
202 | service_start "hostapd@${device}.service" |
203 | local ret=$? | |
204 | ||
49ec20d8 MT |
205 | if [ ${ret} -eq ${EXIT_OK} ]; then |
206 | log DEBUG "hostapd has been successfully started on '${device}'" | |
207 | else | |
208 | log ERROR "Could not start hostapd on '${device}': ${ret}" | |
209 | return ${EXIT_ERROR} | |
210 | fi | |
211 | ||
212 | return ${EXIT_OK} | |
0e035311 MT |
213 | } |
214 | ||
215 | function hostapd_stop() { | |
216 | local device=${1} | |
217 | assert isset device | |
218 | ||
219 | service_stop "hostapd@${device}.service" | |
0e035311 | 220 | } |