Add wireless client for RED to startup process.
[ipfire-2.x.git] / src / initscripts / init.d / wlanclient
CommitLineData
61027579
MT
1#!/bin/sh
2########################################################################
3# Begin $rc_base/init.d/wlan_client
4#
5# Description : Wireless client initscript
6#
7########################################################################
8
9. /etc/sysconfig/rc
10. ${rc_functions}
11
12eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
13
14WIRELESS_CONFIG="/var/ipfire/ethernet/wireless"
15
16function device_is_wireless() {
17 local device=${1}
18
19 if [ -d "/sys/class/net/${device}/wireless" ]; then
20 return 0
21 fi
22
23 return 1
24}
25
26function wpa_supplicant_make_config() {
27 local device=${1}
28 local config=${2}
29 shift 2
30
31 # Check if device is wireless.
32 local wireless="false"
33 if device_is_wireless ${device}; then
34 wireless="true"
35 fi
36
37 # Write a configuration file header.
38 (
39 echo "#"
40 echo "# THIS FILE IS AUTOMATICALLY GENERATED AND"
41 echo "# ANY CUSTOM CHANGES WILL BE OVERWRITTEN!"
42 echo "#"
43 echo
44 echo "ctrl_interface=/var/run/wpa_supplicant"
45 echo
46 ) > ${config}
47
48 local items=0
49
50 local line
51 while IFS="," read -ra line; do
52 # Skip commented lines.
53 [ "${line:0:1}" = "#" ] && continue
54
55 # Skip disabled entries.
56 [ "${line[2]}" = "on" ] || continue
57
58 wpa_supplicant_config_line \
59 ${device} ${config} \
60 --wireless="${wireless}" \
61 --mode="${line[3]}" \
62 --wpa-mode="${line[4]}" \
63 --ssid="${line[5]}" \
64 --psk="${line[6]}" \
65 --priority="${line[7]}"
66
67 items=$(( ${items} + 1 ))
68
69 done < ${WIRELESS_CONFIG}
70
71 # Return exit code 2, when there are no entries in the
72 # configuration file.
73 if [ "${items}" = "0" ]; then
74 return 2
75 fi
76
77 return 0
78}
79
80function wpa_supplicant_config_line() {
81 local device=${1}
82 local config=${2}
83 shift 2
84
85 local auth_alg
86 local proto
87 local key_mgmt
88 local pairwise
89 local group
90 local mode
91 local priority
92 local psk
93 local ssid
94 local wep_tx_keyidx
95 local wep_key0
96 local wireless="true"
97 local wpa_mode
98
99 while [ $# -gt 0 ]; do
100 case "${1}" in
101 --mode=*)
102 mode=${1#--mode=}
103 ;;
104 --priority=*)
105 priority=${1#--priority=}
106 ;;
107 --psk=*)
108 psk=${1#--psk=}
109 ;;
110 --ssid=*)
111 ssid=${1#--ssid=}
112 ;;
113 --wireless=*)
114 wireless=${1#--wireless=}
115 ;;
116 --wpa-mode=*)
117 wpa_mode=${1#--wpa-mode=}
118 ;;
119 esac
120 shift
121 done
122
123 case "${mode}" in
124 WPA2)
125 auth_alg="OPEN"
126 proto="RSN"
127 key_mgmt="WPA-PSK"
128 ;;
129 WPA)
130 auth_alg="OPEN"
131 proto="WPA"
132 key_mgmt="WPA-PSK"
133 ;;
134 WEP)
135 auth_alg="SHARED"
136 key_mgmt="NONE"
137
138 wep_tx_keyidx=0
139 wep_key0=${psk}
140 psk=""
141 ;;
142 NONE)
143 auth_alg="OPEN"
144 key_mgmt="NONE"
145 ;;
146 *)
147 # Unsupported mode.
148 return 1
149 ;;
150 esac
151
152 if [ "${mode}" = "WPA" -o "${mode}" = "WPA2" ]; then
153 case "${wpa_mode}" in
154 CCMP-CCMP)
155 pairwise="CCMP"
156 group="CCMP"
157 ;;
158 CCMP-TKIP)
159 pairwise="CCMP"
160 group="TKIP"
161 ;;
162 TKIP-TKIP)
163 pairwise="TKIP"
164 group="TKIP"
165 ;;
166 *)
167 pairwise="CCMP TKIP"
168 group="CCMP TKIP"
169 ;;
170 esac
171 fi
172
173 (
174 echo "network={"
175
176 if [ -n "${ssid}" ]; then
177 echo " ssid=\"${ssid}\""
178 fi
179 if [ "${wireless}" = "true" ]; then
180 echo " scan_ssid=1"
181 fi
182 if [ -n "${auth_alg}" ]; then
183 echo " auth_alg=${auth_alg}"
184 fi
185 if [ -n "${key_mgmt}" ]; then
186 echo " key_mgmt=${key_mgmt}"
187 fi
188 if [ -n "${psk}" ]; then
189 echo " psk=\"${psk}\""
190 fi
191 if [ -n "${wep_tx_keyidx}" ]; then
192 echo " wep_tx_keyidx=${wep_tx_keyidx}"
193 fi
194 if [ -n "${wep_key0}" ]; then
195 echo " wep_key0=\"${wep_key0}\""
196 fi
197 if [ -n "${proto}" ]; then
198 echo " proto=${proto}"
199 fi
200 if [ -n "${pairwise}" -a -n "${group}" ]; then
201 echo " pairwise=${pairwise}"
202 echo " group=${group}"
203 fi
204 if [ -n "${priority}" ]; then
205 echo " priority=${priority}"
206 fi
207
208 echo "}"
209 echo
210 ) >> ${config}
211}
212
213function wpa_supplicant_start() {
214 local device=${1}
215 local config="/etc/wpa_supplicant.conf"
216
217 # Write configuration file.
218 wpa_supplicant_make_config ${device} ${config}
219 [ $? -eq 0 ] || return 0
220
221 # Build wpa_supplicant command line.
222 local wpa_suppl_cmd="wpa_supplicant -B -qqq -i${device} -c${config}"
223
224 if device_is_wireless ${device}; then
225 wpa_suppl_cmd="${wpa_suppl_cmd} -Dwext"
226 else
227 wpa_suppl_cmd="${wpa_suppl_cmd} -Dwired"
228 fi
229
230 # Run the shiz.
231 boot_mesg "Starting wireless client on ${RED_DEV}..."
232 loadproc ${wpa_suppl_cmd}
233
234 # Run wpa_cli to handle reconnection events.
235 boot_mesg "Starting wireless event handler on ${RED_DEV}..."
236 wpa_cli -B -a /etc/rc.d/init.d/networking/wpa_supplicant.exe
237}
238
239function wpa_supplicant_stop() {
240 boot_mesg "Stopping wireless event handler on ${RED_DEV}..."
241 killproc wpa_cli
242
71ea0d68
SS
243 # wpa_cli does not send a disconnect event when get stopped.
244 # So we manually have to send it to the wpa_supplicant.exe.
245 /etc/rc.d/init.d/networking/wpa_supplicant.exe "${RED_DEV}" DISCONNECTED
246
61027579
MT
247 boot_mesg "Stopping wireless client on ${RED_DEV}..."
248 killproc wpa_supplicant
71ea0d68
SS
249
250 # Tidy up /tmp directory.
251 rm -f /tmp/wpa_ctrl_*
61027579
MT
252}
253
254case "${1}" in
255 start)
256 if [ -n "${RED_DEV}" ] && device_is_wireless ${RED_DEV}; then
257 wpa_supplicant_start ${RED_DEV}
258 fi
259 ;;
260
261 stop)
262 if [ -n "${RED_DEV}" ] && device_is_wireless ${RED_DEV}; then
263 wpa_supplicant_stop
264 fi
265 ;;
266
267 restart)
268 ${0} stop
269 sleep 1
270 ${0} start
271 ;;
272
273 status)
274 statusproc wpa_supplicant
275 ;;
276
277 *)
278 echo "Usage: ${0} {start|stop|restart|status}"
279 exit 1
280 ;;
281esac
282
283# End $rc_base/init.d/wlan_client