]> git.ipfire.org Git - people/ms/network.git/blob - functions.hostapd
1d10b978fff241cd9c1508bef31fc1f143d80a94
[people/ms/network.git] / 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 function 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
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 --country-code=*)
53 country_code=$(cli_get_val ${1})
54 ;;
55 --encryption=*)
56 encryption=$(cli_get_val ${1})
57 ;;
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 ;;
66 --key=*)
67 key=$(cli_get_val ${1})
68 ;;
69 --mode=*)
70 mode=$(cli_get_val ${1})
71 ;;
72 --ssid=*)
73 ssid=$(cli_get_val ${1})
74 ;;
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 ;;
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
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 print
118 ) >> ${file}
119
120 # Wireless configuration
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"
136 else
137 hw_mode="${mode}"
138 fi
139
140 (
141 print "# Wireless configuration"
142 print "channel=${channel}"
143 print "country_code=${country_code}"
144 print "hw_mode=${hw_mode}"
145 print "ieee80211d=${ieee80211d}"
146 print "ieee80211n=${ieee80211n}"
147 print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
148
149 if contains_spaces "${ssid}"; then
150 print "ssid=\"${ssid}\""
151 else
152 print "ssid=${ssid}"
153 fi
154
155 # WMM
156 print "wmm_enabled=${wmm}"
157
158 print
159 ) >> ${file}
160
161 # Control interface.
162 (
163 print "# Control interface"
164 print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}"
165 print "ctrl_interface_group=0"
166 print
167 ) >> ${file}
168
169 # Encryption settings
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
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 print
192 ) >> ${file}
193 fi
194
195 return ${EXIT_OK}
196 }
197
198 function hostapd_start() {
199 local device=${1}
200 assert isset device
201
202 service_start "hostapd@${device}.service"
203 local ret=$?
204
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}
213 }
214
215 function hostapd_stop() {
216 local device=${1}
217 assert isset device
218
219 service_stop "hostapd@${device}.service"
220 }