]> git.ipfire.org Git - people/stevee/network.git/blob - functions.hostapd
wireless: Add function to find DFS channels.
[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 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 key
39 local mode
40 local ssid
41
42 while [ $# -gt 0 ]; do
43 case "${1}" in
44 --broadcast-ssid=*)
45 broadcast_ssid=$(cli_get_val ${1})
46 ;;
47 --channel=*)
48 channel=$(cli_get_val ${1})
49 ;;
50 --country-code=*)
51 country_code=$(cli_get_val ${1})
52 ;;
53 --mode=*)
54 mode=$(cli_get_val ${1})
55 ;;
56 --ssid=*)
57 ssid=$(cli_get_val ${1})
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 # Create configuration directory.
89 local config_dir=$(dirname ${file})
90 mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null
91
92 config_header "hostapd" > ${file}
93
94 # Interface configuration
95 (
96 print "# Interface configuration"
97 print "driver=nl80211"
98 print "interface=${device}"
99 print
100 ) >> ${file}
101
102 # Wireless configuration
103 local ignore_broadcast_ssid
104 if enabled broadcast_ssid; then
105 ignore_broadcast_ssid="0"
106 else
107 ignore_broadcast_ssid="1"
108 fi
109
110 local hw_mode ieee80211n="0"
111 if [ "${mode}" = "n" ]; then
112 if [ ${channel} -le 15 ]; then
113 hw_mode="g"
114 else
115 hw_mode="a"
116 fi
117 ieee80211n="1"
118 else
119 hw_mode="${mode}"
120 fi
121
122 (
123 print "# Wireless configuration"
124 print "channel=${channel}"
125 print "country_code=${country_code}"
126 print "hw_mode=${hw_mode}"
127 print "ieee80211n=${ieee80211n}"
128 print "ignore_broadcast_ssid=${ignore_broadcast_ssid}"
129
130 if contains_spaces "${ssid}"; then
131 print "ssid=\"${ssid}\""
132 else
133 print "ssid=${ssid}"
134 fi
135
136 print
137 ) >> ${file}
138
139 # Control interface.
140 (
141 print "# Control interface"
142 print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}"
143 print "ctrl_interface_group=0"
144 print
145 ) >> ${file}
146
147 # Encryption settings
148 if isset encryption; then
149 local encryption_mode=0
150 case "${encryption}" in
151 WPA)
152 encryption_mode=1
153 ;;
154 WPA2)
155 encryption_mode=2
156 ;;
157 WPA/WPA2)
158 encryption_mode=3
159 ;;
160 esac
161
162 (
163 print "# Encryption settings"
164 print "wpa=${encryption_mode}"
165 print "wpa_passphrase=${key}"
166 print "wpa_key_mgmt=WPA-PSK"
167 print "wpa_pairwise=TKIP"
168 print "rsn_pairwise=CCMP"
169 print
170 ) >> ${file}
171 fi
172
173 return ${EXIT_OK}
174 }
175
176 function hostapd_start() {
177 local device=${1}
178 assert isset device
179
180 service_start "hostapd@${device}.service"
181 local ret=$?
182
183 if [ ${ret} -eq ${EXIT_OK} ]; then
184 log DEBUG "hostapd has been successfully started on '${device}'"
185 else
186 log ERROR "Could not start hostapd on '${device}': ${ret}"
187 return ${EXIT_ERROR}
188 fi
189
190 return ${EXIT_OK}
191 }
192
193 function hostapd_stop() {
194 local device=${1}
195 assert isset device
196
197 service_stop "hostapd@${device}.service"
198 }