]> git.ipfire.org Git - people/stevee/network.git/blob - functions.hostapd
ea44b0aa987c7460f485b3259fc26cbf99aea496
[people/stevee/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 function hostapd_config_dir() {
23 local device=${1}
24
25 echo "${RUN_DIR}/hostapd/${device}"
26 }
27
28 function hostapd_config_write() {
29 local device=${1}
30 shift
31
32 assert device_exists ${device}
33
34 local broadcast_ssid
35 local channel
36 local country_code
37 local encryption
38 local key
39 local mode
40 local ssid
41
42 while [ $# -gt 0 ]; do
43 case "${1}" in
44 --broadcast-ssid=*)
45 broadcast_ssid=${1#--broadcast-ssid=}
46 ;;
47 --channel=*)
48 channel=${1#--channel=}
49 ;;
50 --country-code=*)
51 country_code=${1#--country-code=}
52 ;;
53 --mode=*)
54 mode=${1#--mode=}
55 ;;
56 --ssid=*)
57 ssid=${1#--ssid=}
58 ;;
59 --encryption=*)
60 encryption=$(cli_get_val ${1})
61 ;;
62 --key=*)
63 key=$(cli_get_val ${1})
64 ;;
65 *)
66 warning_log "Ignoring unknown argument '${1}'."
67 ;;
68 esac
69 shift
70 done
71
72 assert isset broadcast_ssid
73 assert isbool broadcast_ssid
74
75 assert isset channel
76 assert isinteger channel
77
78 assert isset country_code
79 assert isset mode
80 assert isset ssid
81
82 # Check if key is set when encryption is used.
83 if isset encryption; then
84 assert isoneof encryption WPA WPA2 WPA/WPA2
85 assert isset key
86 fi
87
88 local ignore_broadcast_ssid
89 if enabled broadcast_ssid; then
90 ignore_broadcast_ssid="0"
91 else
92 ignore_broadcast_ssid="1"
93 fi
94
95 local hw_mode ieee80211n="0"
96 if [ "${mode}" = "n" ]; then
97 if [ ${channel} -le 15 ]; then
98 hw_mode="g"
99 else
100 hw_mode="a"
101 fi
102 ieee80211n="1"
103 fi
104
105 cat <<EOF
106 ### Hostapd configuration for ${device}
107
108 # Interface configuration
109 driver=nl80211
110 interface=${device}
111
112 # Wireless configuration
113 channel=${channel}
114 country_code=${country_code}
115 hw_mode=${hw_mode}
116 ieee80211n=${ieee80211n}
117 ignore_broadcast_ssid=${ignore_broadcast_ssid}
118 ssid=${ssid}
119
120 # Dump file
121 dump_file=$(hostapd_config_dir ${device}/dump)
122
123 ctrl_interface=/var/run/hostapd
124 ctrl_interface_group=0
125
126 EOF
127
128 if isset encryption; then
129 local encryption_mode=0
130 case "${encryption}" in
131 WPA)
132 encryption_mode=1
133 ;;
134 WPA2)
135 encryption_mode=2
136 ;;
137 WPA/WPA2)
138 encryption_mode=3
139 ;;
140 esac
141
142 print "# Encryption settings."
143 print "wpa=${encryption_mode}"
144 print "wpa_passphrase=${key}"
145 print "wpa_key_mgmt=WPA-PSK"
146 print "wpa_pairwise=TKIP"
147 print "rsn_pairwise=CCMP"
148 print
149 fi
150
151 return ${EXIT_OK}
152 }
153
154 function hostapd_start() {
155 local device=${1}
156 shift
157
158 assert isset device
159
160 local config_dir=$(hostapd_config_dir ${device})
161 mkdir -p ${config_dir}
162
163 local config_file=${config_dir}/config
164 hostapd_config_write ${device} $@ > ${config_file}
165
166 service_start "hostapd@${device}.service"
167 local ret=$?
168
169 case "${ret}" in
170 0)
171 log DEBUG "Hostapd was successfully started for '${device}'."
172 return ${EXIT_OK}
173 ;;
174 1)
175 error_log "Could not start hostapd properly for '${device}'."
176
177 error_log "Configuration file dump:"
178 local line
179 while read line; do
180 error_log " ${line}"
181 done < ${config_file}
182
183 return ${EXIT_ERROR}
184 ;;
185 esac
186 }
187
188 function hostapd_stop() {
189 local device=${1}
190 assert isset device
191
192 service_stop "hostapd@${device}.service"
193
194 rm -rf $(hostapd_config_dir ${device})
195 }
196
197 function hostapd_get_pid() {
198 local device=${1}
199
200 assert isset device
201
202 local pid_file="$(hostapd_config_dir ${device})/pid"
203
204 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
205
206 cat ${pid_file} 2>/dev/null
207 return ${EXIT_OK}
208 }
209
210 function hostapd_is_running() {
211 local device=${1}
212
213 assert isset device
214
215 local pid=$(hostapd_get_pid ${device})
216
217 if isset pid && [ -d "/proc/${pid}" ]; then
218 return ${EXIT_OK}
219 fi
220
221 return ${EXIT_ERROR}
222 }