]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.wireless
network: Experimental support for wireless access points.
[people/arne_f/network.git] / functions.wireless
1 #!/bin/bash
2 # XXX header missing
3
4 PHY_DIR="/sys/class/ieee80211"
5
6 function phy_dir() {
7 local phy=${1}
8
9 echo "${PHY_DIR}/${phy}"
10 }
11
12 function phy_exists() {
13 local phy=${1}
14
15 [ -d "$(phy_dir ${phy})" ]
16 }
17
18 function phy_list() {
19 local phy
20 for phy in $(phy_dir)/*; do
21 phy=$(basename ${phy})
22 echo "${phy}"
23 done
24 }
25
26 function phy_get() {
27 local info=${1}
28
29 local phy
30
31 if listmatch ${info} $(phy_list); then
32 phy="${info}"
33 elif device_exists ${info}; then
34 info=$(device_get_address ${info})
35 fi
36
37 if [ -z "${phy}" ] && mac_is_valid ${info}; then
38 local i
39 for i in $(phy_list); do
40 if [ "${info}" = "$(phy_get_address ${i})" ]; then
41 phy=${i}
42 break
43 fi
44 done
45 fi
46
47 if [ -z "${phy}" ]; then
48 return ${EXIT_ERROR}
49 fi
50
51 echo "${phy}"
52 return ${EXIT_OK}
53 }
54
55 function phy_get_address() {
56 local phy=${1}
57
58 assert isset phy
59
60 cat $(phy_dir ${phy})/macaddress 2>/dev/null
61 }
62
63 function wireless_create() {
64 local device=${1}
65 local phy=$(phy_get ${2})
66 local type=${3}
67 local mac=${4}
68
69 assert isset device
70 assert isset phy
71 assert isset type
72
73 isset mac || mac=$(mac_generate)
74
75 assert phy_exists ${phy}
76 assert isoneof type managed __ap
77
78 iw phy ${phy} interface add ${device} type ${type}
79
80 if device_exists ${device}; then
81 device_set_address ${device} ${mac}
82 fi
83
84 device_set_up ${device}
85 }
86
87 function wireless_remove() {
88 local device=${1}
89
90 assert device_exists ${device}
91
92 device_set_down ${device}
93
94 iw dev ${device} del
95 }
96
97 function wireless_set_channel() {
98 local device=${1}
99 local channel=${2}
100
101 assert isset device
102 assert device_exists ${device}
103 assert isset channel
104
105 iw dev ${device} set channel ${channel} $@
106 }
107
108 function hostapd_init() {
109 mkdir -p $(hostapd_config_dir)
110 }
111
112 init_register hostapd_init
113
114 function hostapd_config_dir() {
115 local device=${1}
116
117 echo "${RUN_DIR}/hostapd/${device}"
118 }
119
120 function hostapd_config_write() {
121 local device=${1}
122 shift
123
124 assert device_exists ${device}
125
126 local broadcast_ssid
127 local channel
128 local country_code
129 local mode
130 local ssid
131
132 while [ $# -gt 0 ]; do
133 case "${1}" in
134 --broadcast-ssid=*)
135 broadcast_ssid=${1#--broadcast-ssid=}
136 ;;
137 --channel=*)
138 channel=${1#--channel=}
139 ;;
140 --country-code=*)
141 country_code=${1#--country-code=}
142 ;;
143 --mode=*)
144 mode=${1#--mode=}
145 ;;
146 --ssid=*)
147 ssid=${1#--ssid=}
148 ;;
149 *)
150 warning_log "Ignoring unknown argument '${1}'."
151 ;;
152 esac
153 shift
154 done
155
156 assert isset broadcast_ssid
157 assert isbool broadcast_ssid
158
159 assert isset channel
160 assert isinteger channel
161
162 assert isset country_code
163 assert isset mode
164 assert isset ssid
165
166 local ignore_broadcast_ssid
167 if enabled broadcast_ssid; then
168 ignore_broadcast_ssid="0"
169 else
170 ignore_broadcast_ssid="1"
171 fi
172
173 cat <<EOF
174 ### Hostapd configuration for ${device}
175
176 # Interface configuration
177 driver=nl80211
178 interface=${device}
179
180 # Wireless configuration
181 channel=${channel}
182 country_code=${country_code}
183 hw_mode=${mode}
184 ignore_broadcast_ssid=${ignore_broadcast_ssid}
185 ssid=${ssid}
186
187 # Logging options
188 logger_syslog=-1
189 logger_syslog_level=2
190 logger_stdout=-1
191 logger_stdout_level=2
192
193 # Dump file
194 dump_file=$(hostapd_config_dir ${device}/dump
195
196 ctrl_interface=/var/run/hostapd
197 ctrl_interface_group=0
198 EOF
199
200 return ${EXIT_OK}
201 }
202
203 function hostapd_start() {
204 local device=${1}
205 shift
206
207 assert isset device
208
209 local config_dir=$(hostapd_config_dir ${device})
210 mkdir -p ${config_dir}
211
212 local config_file=${config_dir}/config
213 hostapd_config_write ${device} $@ > ${config_file}
214
215 hostapd -dd -B -P ${config_dir}/pid ${config_file}
216 local ret=$?
217
218 case "${ret}" in
219 0)
220 log DEBUG "Hostapd was successfully started for '${device}'."
221 return ${EXIT_OK}
222 ;;
223 1)
224 error_log "Could not start hostapd properly for '${device}'."
225
226 error_log "Configuration file dump:"
227 local line
228 while read line; do
229 error_log " ${line}"
230 done < ${config_file}
231
232 return ${EXIT_ERROR}
233 ;;
234 esac
235 }
236
237 function hostapd_stop() {
238 local device=${1}
239
240 assert isset device
241
242 local pid=$(hostapd_get_pid ${device})
243
244 if isset pid; then
245 process_kill ${pid}
246 else
247 warning_log "Could not find pid file for hostapd process running for ${device}."
248 fi
249
250 rm -rf $(hostapd_config_dir ${device})
251 }
252
253 function hostapd_get_pid() {
254 local device=${1}
255
256 assert isset device
257
258 local pid_file="$(hostapd_config_dir ${device})/pid"
259
260 [ -e "${pid_file}" ] || return ${EXIT_ERROR}
261
262 cat ${pid_file} 2>/dev/null
263 return ${EXIT_OK}
264 }
265
266 function hostapd_is_running() {
267 local device=${1}
268
269 assert isset device
270
271 local pid=$(hostapd_get_pid ${device})
272
273 if isset pid && [ -d "/proc/${pid}" ]; then
274 return ${EXIT_OK}
275 fi
276
277 return ${EXIT_ERROR}
278 }