]> git.ipfire.org Git - people/stevee/network.git/blob - functions.hostapd
hostapd: Fix empty hw_mode variable.
[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 else
104 hw_mode="${mode}"
105 fi
106
107 cat <<EOF
108 ### Hostapd configuration for ${device}
109
110 # Interface configuration
111 driver=nl80211
112 interface=${device}
113
114 # Wireless configuration
115 channel=${channel}
116 country_code=${country_code}
117 hw_mode=${hw_mode}
118 ieee80211n=${ieee80211n}
119 ignore_broadcast_ssid=${ignore_broadcast_ssid}
120 ssid=${ssid}
121
122 # Dump file
123 dump_file=$(hostapd_config_dir ${device}/dump)
124
125 ctrl_interface=/var/run/hostapd
126 ctrl_interface_group=0
127
128 EOF
129
130 if isset encryption; then
131 local encryption_mode=0
132 case "${encryption}" in
133 WPA)
134 encryption_mode=1
135 ;;
136 WPA2)
137 encryption_mode=2
138 ;;
139 WPA/WPA2)
140 encryption_mode=3
141 ;;
142 esac
143
144 print "# Encryption settings."
145 print "wpa=${encryption_mode}"
146 print "wpa_passphrase=${key}"
147 print "wpa_key_mgmt=WPA-PSK"
148 print "wpa_pairwise=TKIP"
149 print "rsn_pairwise=CCMP"
150 print
151 fi
152
153 return ${EXIT_OK}
154 }
155
156 function hostapd_start() {
157 local device=${1}
158 shift
159
160 assert isset device
161
162 local config_dir=$(hostapd_config_dir ${device})
163 mkdir -p ${config_dir}
164
165 local config_file=${config_dir}/config
166 hostapd_config_write ${device} $@ > ${config_file}
167
168 service_start "hostapd@${device}.service"
169 local ret=$?
170
171 case "${ret}" in
172 0)
173 log DEBUG "Hostapd was successfully started for '${device}'."
174 return ${EXIT_OK}
175 ;;
176 1)
177 error_log "Could not start hostapd properly for '${device}'."
178
179 error_log "Configuration file dump:"
180 local line
181 while read line; do
182 error_log " ${line}"
183 done < ${config_file}
184
185 return ${EXIT_ERROR}
186 ;;
187 esac
188 }
189
190 function hostapd_stop() {
191 local device=${1}
192 assert isset device
193
194 service_stop "hostapd@${device}.service"
195
196 rm -rf $(hostapd_config_dir ${device})
197 }