1# Start, reconfigure and stop wpa_supplicant per wireless interface.
2#
3# This is only needed when using wpa_supplicant-2.5 or older, OR
4# when wpa_supplicant has not been built with CONFIG_MATCH_IFACE, OR
5# wpa_supplicant was launched without the -M flag to activate
6# interface matching.
7
8if [ -z "$wpa_supplicant_conf" ]; then
9	for x in \
10		/etc/wpa_supplicant/wpa_supplicant-"$interface".conf \
11		/etc/wpa_supplicant/wpa_supplicant.conf \
12		/etc/wpa_supplicant-"$interface".conf \
13		/etc/wpa_supplicant.conf \
14	; do
15		if [ -s "$x" ]; then
16			wpa_supplicant_conf="$x"
17			break
18		fi
19	done
20fi
21: ${wpa_supplicant_conf:=/etc/wpa_supplicant.conf}
22
23wpa_supplicant_ctrldir()
24{
25	dir=$(key_get_value "[[:space:]]*ctrl_interface=" \
26		"$wpa_supplicant_conf")
27	dir=$(trim "$dir")
28	case "$dir" in
29	DIR=*)
30		dir=${dir##DIR=}
31		dir=${dir%%[[:space:]]GROUP=*}
32		dir=$(trim "$dir")
33		;;
34	esac
35	printf %s "$dir"
36}
37
38wpa_supplicant_start()
39{
40	# If the carrier is up, don't bother checking anything
41	[ "$ifcarrier" = "up" ] && return 0
42
43	# Pre flight checks
44	if [ ! -s "$wpa_supplicant_conf" ]; then
45		syslog warn \
46			"$wpa_supplicant_conf does not exist"
47		syslog warn "not interacting with wpa_supplicant(8)"
48		return 1
49	fi
50	dir=$(wpa_supplicant_ctrldir)
51	if [ -z "$dir" ]; then
52		syslog warn \
53			"ctrl_interface not defined in $wpa_supplicant_conf"
54		syslog warn "not interacting with wpa_supplicant(8)"
55		return 1
56	fi
57
58	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 && return 0
59	syslog info "starting wpa_supplicant"
60	driver=${wpa_supplicant_driver:+-D}$wpa_supplicant_driver
61	err=$(wpa_supplicant -B -c"$wpa_supplicant_conf" -i"$interface" \
62	    "$driver" 2>&1)
63	errn=$?
64	if [ $errn != 0 ]; then
65		syslog err "failed to start wpa_supplicant"
66		syslog err "$err"
67	fi
68	return $errn
69}
70
71wpa_supplicant_reconfigure()
72{
73	dir=$(wpa_supplicant_ctrldir)
74	[ -z "$dir" ] && return 1
75	if ! wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1; then
76		wpa_supplicant_start
77		return $?
78	fi
79	syslog info "reconfiguring wpa_supplicant"
80	err=$(wpa_cli -p "$dir" -i "$interface" reconfigure 2>&1)
81	errn=$?
82	if [ $errn != 0 ]; then
83		syslog err "failed to reconfigure wpa_supplicant"
84		syslog err "$err"
85	fi
86	return $errn
87}
88
89wpa_supplicant_stop()
90{
91	dir=$(wpa_supplicant_ctrldir)
92	[ -z "$dir" ] && return 1
93	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 || return 0
94	syslog info "stopping wpa_supplicant"
95	err=$(wpa_cli -i"$interface" terminate 2>&1)
96	errn=$?
97	if [ $errn != 0 ]; then
98		syslog err "failed to stop wpa_supplicant"
99		syslog err "$err"
100	fi
101	return $errn
102}
103
104if [ "$ifwireless" = "1" ] && \
105    type wpa_supplicant >/dev/null 2>&1 && \
106    type wpa_cli >/dev/null 2>&1
107then
108	case "$reason" in
109	PREINIT)	wpa_supplicant_start;;
110	RECONFIGURE)	wpa_supplicant_reconfigure;;
111	DEPARTED)	wpa_supplicant_stop;;
112	esac
113fi
114