slitaz-boot-scripts view etc/init.d/network.sh @ rev 396

network.sh: kinda we can now start and stop connections, as well to re-connect wlan <-> eth without stopping, as well to connect hidden network not knowing it's bssid.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Mar 25 00:29:33 2015 +0200 (2015-03-25)
parents 7acd64a8f538
children 28a9c8b64212
line source
1 #!/bin/sh
2 #
3 # /etc/init.d/network.sh : Network initialization boot script
4 # /etc/network.conf : Main SliTaz network configuration file
5 # /etc/wpa/wpa.conf : Wi-Fi networks configuration file
7 . /etc/init.d/rc.functions
9 CONF="${2:-/etc/network.conf}"
10 echo "Loading network settings from $CONF"
11 . "$CONF"
13 WPA_CONF='/etc/wpa/wpa.conf'
15 # Migrate existing settings to a new format file
17 . /usr/share/slitaz/network.conf_migration
20 # Actions executing on boot time (running network.sh without parameters)
22 boot() {
23 # Set hostname
24 echo -n "Setting hostname to: $(cat /etc/hostname)"
25 /bin/hostname -F /etc/hostname
26 status
28 # Configure loopback interface
29 echo -n 'Configuring loopback...'
30 /sbin/ifconfig lo 127.0.0.1 up
31 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
32 status
34 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
35 }
38 # Use ethernet
40 eth() {
41 [ "$WIFI" != 'yes' ] && ifconfig $INTERFACE up & sleep 5
42 }
45 # Start wpa_supplicant with prepared settings in wpa.conf
47 start_wpa_supplicant() {
48 echo "Starting wpa_supplicant for $1..."
49 wpa_supplicant -B -W -c$WPA_CONF -D$WIFI_WPA_DRIVER -i$WIFI_INTERFACE
50 }
53 # Reconnect to the given network
55 reconnect_wifi_network() {
56 if [ "$WIFI" == 'yes' ]; then
57 # Wpa_supplicant will auto-connect to the first network
58 # notwithstanding to priority when scan_ssid=1
59 current_ssid="$(wpa_cli list_networks 2>/dev/null | fgrep '[CURRENT]' | cut -f2)"
60 if [ "$current_ssid" != "$WIFI_ESSID" ]; then
61 echo "Connecting to $WIFI_ESSID..."
62 for i in $(seq 20); do
63 index=$(wpa_cli list_networks 2>/dev/null | \
64 grep -m1 -F $'\t'$WIFI_ESSID$'\t' | head -n1 | cut -f1)
65 [ -z "$index" ] && echo -n '.' && sleep 1
66 done
67 wpa_cli select_network $index >/dev/null; status
68 fi
69 fi
70 }
73 # For Wi-Fi. Users just have to enable it through WIFI="yes" and usually
74 # ESSID="any" will work and the interface is autodetected.
76 wifi() {
77 if [ "$WIFI" == 'yes' ]; then
78 ifconfig $INTERFACE down
80 # Confirm if $WIFI_INTERFACE is the Wi-Fi interface
81 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
82 echo "$WIFI_INTERFACE is not a Wi-Fi interface, changing it."
83 WIFI_INTERFACE=$(iwconfig 2>/dev/null | awk 'NR==1{print $1}')
84 [ -n "$WIFI_INTERFACE" ] && sed -i \
85 "s|^WIFI_INTERFACE=.*|WIFI_INTERFACE=\"$WIFI_INTERFACE\"|" \
86 /etc/network.conf
87 fi
89 echo -n "Configuring Wi-Fi interface $WIFI_INTERFACE..."
90 ifconfig $WIFI_INTERFACE up 2>/dev/null
91 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
92 iwconfig $WIFI_INTERFACE txpower on
93 fi
94 status
96 IWCONFIG_ARGS=''
97 [ -n "$WIFI_WPA_DRIVER" ] || WIFI_WPA_DRIVER='wext'
98 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
99 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
100 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
102 # Clean all / add / change stored networks settings
103 if [ "$WIFI_BLANK_NETWORKS" == 'yes' ]; then
104 echo "Creating new $WPA_CONF"
105 cat /etc/wpa/wpa_empty.conf > $WPA_CONF
106 else
107 if fgrep -q ssid=\"$WIFI_ESSID\" $WPA_CONF; then
108 echo "Change network settings in $WPA_CONF"
109 # Remove given existing network (it's to be appended later)
110 mv -f $WPA_CONF $WPA_CONF.old
111 cat $WPA_CONF.old | tr '\n' '\a' | sed 's|[^#]\(network={\)|\n\1|g' | \
112 fgrep -v "ssid=\"$WIFI_ESSID\"" | tr '\a' '\n' > $WPA_CONF
113 else
114 echo "Append existing $WPA_CONF"
115 fi
116 fi
118 # Each new network has a higher priority than the existing
119 MAX_PRIORITY=$(sed -n 's|[\t ]*priority=\([0-9]*\)|\1|p' $WPA_CONF | sort -g | tail -n1)
120 PRIORITY=$(( ${MAX_PRIORITY:-0} + 1 ))
122 # Begin network description
123 cat >> $WPA_CONF <<EOT
124 network={
125 ssid="$WIFI_ESSID"
126 EOT
128 # For networks with hidden SSID: write its BSSID
129 [ -n "$WIFI_BSSID" ] && cat >> $WPA_CONF <<EOT
130 bssid=$WIFI_BSSID
131 EOT
132 # Allow probe requests (for all networks)
133 cat >> $WPA_CONF <<EOT
134 scan_ssid=1
135 EOT
137 case x$(echo -n $WIFI_KEY_TYPE | tr a-z A-Z) in
138 x|xNONE) # Open network
139 cat >> $WPA_CONF <<EOT
140 key_mgmt=NONE
141 priority=$PRIORITY
142 }
143 EOT
144 # start_wpa_supplicant NONE
145 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
146 ;;
148 xWEP) # WEP security
149 # Encryption key length: 64 bit (5 ASCII or 10 HEX)
150 # Encryption key length: 128 bit (13 ASCII or 26 HEX)
151 # ASCII key in "quotes", HEX key without quotes
152 case "${#WIFI_KEY}" in
153 10|26) Q='' ;;
154 *) Q='"' ;;
155 esac
156 cat >> $WPA_CONF <<EOT
157 key_mgmt=NONE
158 auth_alg=OPEN SHARED
159 wep_key0=$Q$WIFI_KEY$Q
160 priority=$PRIORITY
161 }
162 EOT
163 start_wpa_supplicant WEP ;;
165 xWPA) # WPA/WPA2-PSK security
166 cat >> $WPA_CONF <<EOT
167 psk="$WIFI_KEY"
168 key_mgmt=WPA-PSK
169 priority=$PRIORITY
170 }
171 EOT
172 start_wpa_supplicant WPA/WPA2-PSK ;;
174 xEAP) # 802.1x EAP security
175 {
176 cat <<EOT
177 key_mgmt=WPA-EAP IEEE8021X
178 eap=$WIFI_EAP_METHOD
179 EOT
180 if [ "$WIFI_EAP_METHOD" == 'PWD' ]; then
181 WIFI_PHASE2=''; WIFI_CA_CERT=''; WIFI_USER_CERT=''; WIFI_ANONYMOUS_IDENTITY=''
182 fi
183 [ -n "$WIFI_CA_CERT" ] && echo -e "\tca_cert=\"$WIFI_CA_CERT\""
184 [ -n "$WIFI_CLIENT_CERT" ] && echo -e "\tclient_cert=\"$WIFI_CLIENT_CERT\""
185 [ -n "$WIFI_IDENTITY" ] && echo -e "\tidentity=\"$WIFI_IDENTITY\""
186 [ -n "$WIFI_ANONYMOUS_IDENTITY" ] && echo -e "\tanonymous_identity=\"$WIFI_ANONYMOUS_IDENTITY\""
187 [ -n "$WIFI_KEY" ] && echo -e "\tpassword=\"$WIFI-KEY\""
188 [ -n "$WIFI_PHASE2" ] && echo -e "\tphase2=\"auth=$WIFI_PHASE2\""
189 } >> $WPA_CONF
190 start_wpa_supplicant '802.1x EAP' ;;
192 xANY)
193 cat >> $WPA_CONF <<EOT
194 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
195 group=CCMP TKIP WEP104 WEP40
196 pairwise=CCMP TKIP
197 psk="$WIFI_KEY"
198 password="$WIFI_KEY"
199 priority=$PRIORITY
200 }
201 EOT
202 start_wpa_supplicant 'any key type' ;;
204 esac
205 INTERFACE=$WIFI_INTERFACE
206 fi
207 }
210 # WPA DHCP script
212 wpa() {
213 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
214 }
217 # For a dynamic IP with DHCP
219 dhcp() {
220 if [ "$DHCP" == 'yes' ]; then
221 echo "Starting udhcpc client on: $INTERFACE..."
222 # Is wpa wireless && wpa_ctrl_open interface up?
223 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" == 'yes' ]; then
224 wpa
225 else
226 # fallback on udhcpc: wep, eth
227 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
228 /var/run/udhcpc.$INTERFACE.pid
229 fi
230 fi
231 }
234 # For a static IP
236 static_ip() {
237 if [ "$STATIC" == 'yes' ]; then
238 echo "Configuring static IP on $INTERFACE: $IP..."
239 if [ -n "$BROADCAST" ]; then
240 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
241 else
242 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
243 fi
245 # Use ip to set gateways if iproute.conf exists
246 if [ -f /etc/iproute.conf ]; then
247 while read line; do
248 ip route add $line
249 done < /etc/iproute.conf
250 else
251 /sbin/route add default gateway $GATEWAY
252 fi
254 # wpa_supplicant waits for wpa_cli
255 [ -d /var/run/wpa_supplicant ] && wpa_cli -B
257 # Multi-DNS server in $DNS_SERVER
258 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
259 {
260 printf 'nameserver %s\n' $DNS_SERVER # Multiple allowed
261 [ -n "$DOMAIN" ] && echo "search $DOMAIN"
262 } >> /etc/resolv.conf
263 for HELPER in /etc/ipup.d/*; do
264 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
265 done
266 fi
267 }
270 # Stopping everything
272 stop() {
273 echo 'Stopping all interfaces'
274 for iface in $(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d'); do
275 ifconfig $iface down
276 done
277 ifconfig $WIFI_INTERFACE down
279 echo 'Killing all daemons'
280 killall udhcpc
281 killall wpa_supplicant 2>/dev/null
283 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
284 echo 'Shutting down Wi-Fi card'
285 iwconfig $WIFI_INTERFACE txpower off
286 fi
287 }
290 start() {
291 stop
292 eth; wifi
293 dhcp; static_ip
294 reconnect_wifi_network
296 # change default LXPanel panel iface
297 if [ -f /etc/lxpanel/default/panels/panel ]; then
298 sed -i "s/iface=.*/iface=$INTERFACE/" /etc/lxpanel/default/panels/panel
299 fi
300 }
303 # Looking for arguments:
305 case "$1" in
306 '')
307 boot; start ;;
308 start)
309 start ;;
310 stop)
311 stop ;;
312 restart)
313 stop; sleep 2; start ;;
314 *)
315 cat <<EOT
317 $(boldify 'Usage:') /etc/init.d/$(basename $0) [start|stop|restart]
319 Default configuration file is $(boldify '/etc/network.conf')
320 You can specify another configuration file in the second argument:
321 /etc/init.d/$(basename $0) [start|stop|restart] file.conf
323 EOT
324 ;;
325 esac