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

/etc/init.d/bootopts.sh: typo
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Aug 29 21:12:39 2015 +0200 (2015-08-29)
parents 0c9119dea7b8
children 29ec74a6d359
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'
14 [ ! -e "$WPA_CONF" ] && cp /etc/wpa/wpa_empty.conf $WPA_CONF 2> /dev/null
16 # Migrate existing settings to a new format file
18 . /usr/share/slitaz/network.conf_migration
21 # Actions executing on boot time (running network.sh without parameters)
23 boot() {
24 # Set hostname
25 echo -n "Setting hostname to: $(cat /etc/hostname)"
26 /bin/hostname -F /etc/hostname
27 status
29 # Configure loopback interface
30 echo -n 'Configuring loopback...'
31 /sbin/ifconfig lo 127.0.0.1 up
32 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
33 status
35 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
36 }
39 # Use ethernet
41 eth() {
42 [ "$WIFI" != 'yes' ] && ifconfig $INTERFACE up && sleep 5
43 }
46 # Start wpa_supplicant with prepared settings in wpa.conf
48 start_wpa_supplicant() {
49 echo "Starting wpa_supplicant for $1..."
50 wpa_supplicant -B -W -c$WPA_CONF -D$WIFI_WPA_DRIVER -i$WIFI_INTERFACE
51 }
54 # Reconnect to the given network
56 reconnect_wifi_network() {
57 if [ "$WIFI" == 'yes' ]; then
58 # Wpa_supplicant will auto-connect to the first network
59 # notwithstanding to priority when scan_ssid=1
60 current_ssid="$(wpa_cli list_networks 2>/dev/null | fgrep '[CURRENT]' | cut -f2)"
61 if [ "$current_ssid" != "$WIFI_ESSID" ]; then
62 echo "Connecting to $WIFI_ESSID..."
63 for i in $(seq 5); do
64 index=$(wpa_cli list_networks 2>/dev/null | \
65 grep -m1 -F $'\t'$WIFI_ESSID$'\t' | head -n1 | cut -f1)
66 [ -z "$index" ] && echo -n '.' && sleep 1
67 done
68 wpa_cli select_network $index >/dev/null; status
69 fi
70 fi
71 }
74 # Remove selected network settings from wpa.conf
76 remove_network() {
77 mv -f $WPA_CONF $WPA_CONF.old
78 cat $WPA_CONF.old | tr '\n' '\a' | sed 's|[^#]\(network={\)|\n\1|g' | \
79 fgrep -v "ssid=\"$1\"" | tr '\a' '\n' > $WPA_CONF
80 }
83 # For Wi-Fi. Users just have to enable it through WIFI="yes" and usually
84 # ESSID="any" will work and the interface is autodetected.
86 wifi() {
87 if [ "$WIFI" == 'yes' ]; then
88 ifconfig $INTERFACE down
90 # Confirm if $WIFI_INTERFACE is the Wi-Fi interface
91 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
92 echo "$WIFI_INTERFACE is not a Wi-Fi interface, changing it."
93 WIFI_INTERFACE=$(iwconfig 2>/dev/null | awk 'NR==1{print $1}')
94 [ -n "$WIFI_INTERFACE" ] && sed -i \
95 "s|^WIFI_INTERFACE=.*|WIFI_INTERFACE=\"$WIFI_INTERFACE\"|" \
96 /etc/network.conf
97 fi
99 echo -n "Configuring Wi-Fi interface $WIFI_INTERFACE..."
100 ifconfig $WIFI_INTERFACE up 2>/dev/null
101 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
102 iwconfig $WIFI_INTERFACE txpower on
103 fi
104 status
106 IWCONFIG_ARGS=''
107 [ -n "$WIFI_WPA_DRIVER" ] || WIFI_WPA_DRIVER='wext'
108 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
109 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
110 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
112 # Use "any" network only when it is needed
113 [ "$WIFI_ESSID" != 'any' ] && remove_network 'any'
115 # Clean all / add / change stored networks settings
116 if [ "$WIFI_BLANK_NETWORKS" == 'yes' ]; then
117 echo "Creating new $WPA_CONF"
118 cat /etc/wpa/wpa_empty.conf > $WPA_CONF
119 else
120 if fgrep -q ssid=\"$WIFI_ESSID\" $WPA_CONF; then
121 echo "Change network settings in $WPA_CONF"
122 # Remove given existing network (it's to be appended later)
123 remove_network "$WIFI_ESSID"
124 else
125 echo "Append existing $WPA_CONF"
126 fi
127 fi
129 # Each new network has a higher priority than the existing
130 MAX_PRIORITY=$(sed -n 's|[\t ]*priority=\([0-9]*\)|\1|p' $WPA_CONF | sort -g | tail -n1)
131 PRIORITY=$(( ${MAX_PRIORITY:-0} + 1 ))
133 # Begin network description
134 cat >> $WPA_CONF <<EOT
135 network={
136 ssid="$WIFI_ESSID"
137 EOT
139 # For networks with hidden SSID: write its BSSID
140 [ -n "$WIFI_BSSID" ] && cat >> $WPA_CONF <<EOT
141 bssid=$WIFI_BSSID
142 EOT
143 # Allow probe requests (for all networks)
144 cat >> $WPA_CONF <<EOT
145 scan_ssid=1
146 EOT
148 case x$(echo -n $WIFI_KEY_TYPE | tr a-z A-Z) in
149 x|xNONE) # Open network
150 cat >> $WPA_CONF <<EOT
151 key_mgmt=NONE
152 priority=$PRIORITY
153 }
154 EOT
155 # start_wpa_supplicant NONE
156 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
157 ;;
159 xWEP) # WEP security
160 # Encryption key length: 64 bit (5 ASCII or 10 HEX)
161 # Encryption key length: 128 bit (13 ASCII or 26 HEX)
162 # ASCII key in "quotes", HEX key without quotes
163 case "${#WIFI_KEY}" in
164 10|26) Q='' ;;
165 *) Q='"' ;;
166 esac
167 cat >> $WPA_CONF <<EOT
168 key_mgmt=NONE
169 auth_alg=OPEN SHARED
170 wep_key0=$Q$WIFI_KEY$Q
171 priority=$PRIORITY
172 }
173 EOT
174 start_wpa_supplicant WEP ;;
176 xWPA) # WPA/WPA2-PSK security
177 cat >> $WPA_CONF <<EOT
178 psk="$WIFI_KEY"
179 key_mgmt=WPA-PSK
180 priority=$PRIORITY
181 }
182 EOT
183 start_wpa_supplicant WPA/WPA2-PSK ;;
185 xEAP) # 802.1x EAP security
186 {
187 cat <<EOT
188 key_mgmt=WPA-EAP IEEE8021X
189 eap=$WIFI_EAP_METHOD
190 EOT
191 if [ "$WIFI_EAP_METHOD" == 'PWD' ]; then
192 WIFI_PHASE2=''; WIFI_CA_CERT=''; WIFI_USER_CERT=''; WIFI_ANONYMOUS_IDENTITY=''
193 fi
194 [ -n "$WIFI_CA_CERT" ] && echo -e "\tca_cert=\"$WIFI_CA_CERT\""
195 [ -n "$WIFI_CLIENT_CERT" ] && echo -e "\tclient_cert=\"$WIFI_CLIENT_CERT\""
196 [ -n "$WIFI_IDENTITY" ] && echo -e "\tidentity=\"$WIFI_IDENTITY\""
197 [ -n "$WIFI_ANONYMOUS_IDENTITY" ] && echo -e "\tanonymous_identity=\"$WIFI_ANONYMOUS_IDENTITY\""
198 [ -n "$WIFI_KEY" ] && echo -e "\tpassword=\"$WIFI-KEY\""
199 [ -n "$WIFI_PHASE2" ] && echo -e "\tphase2=\"auth=$WIFI_PHASE2\""
200 } >> $WPA_CONF
201 start_wpa_supplicant '802.1x EAP' ;;
203 xANY)
204 cat >> $WPA_CONF <<EOT
205 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
206 group=CCMP TKIP WEP104 WEP40
207 pairwise=CCMP TKIP
208 psk="$WIFI_KEY"
209 password="$WIFI_KEY"
210 priority=$PRIORITY
211 }
212 EOT
213 start_wpa_supplicant 'any key type' ;;
215 esac
216 INTERFACE=$WIFI_INTERFACE
217 fi
218 }
221 # WPA DHCP script
223 wpa() {
224 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
225 }
228 # For a dynamic IP with DHCP
230 dhcp() {
231 if [ "$DHCP" == 'yes' ]; then
232 echo "Starting udhcpc client on: $INTERFACE..."
233 # Is wpa wireless && wpa_ctrl_open interface up?
234 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" == 'yes' ]; then
235 wpa
236 else
237 # fallback on udhcpc: wep, eth
238 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
239 /var/run/udhcpc.$INTERFACE.pid
240 fi
241 fi
242 }
245 # For a static IP
247 static_ip() {
248 if [ "$STATIC" == 'yes' ]; then
249 echo "Configuring static IP on $INTERFACE: $IP..."
250 if [ -n "$BROADCAST" ]; then
251 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
252 else
253 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
254 fi
256 # Use ip to set gateways if iproute.conf exists
257 if [ -f /etc/iproute.conf ]; then
258 while read line; do
259 ip route add $line
260 done < /etc/iproute.conf
261 else
262 /sbin/route add default gateway $GATEWAY
263 fi
265 # wpa_supplicant waits for wpa_cli
266 [ -d /var/run/wpa_supplicant ] && wpa_cli -B
268 # Multi-DNS server in $DNS_SERVER
269 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
270 {
271 printf 'nameserver %s\n' $DNS_SERVER # Multiple allowed
272 [ -n "$DOMAIN" ] && echo "search $DOMAIN"
273 } >> /etc/resolv.conf
274 for HELPER in /etc/ipup.d/*; do
275 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
276 done
277 fi
278 }
281 # Stopping everything
283 stop() {
284 echo 'Stopping all interfaces'
285 for iface in $(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d'); do
286 ifconfig $iface down
287 done
288 ifconfig $WIFI_INTERFACE down
290 echo 'Killing all daemons'
291 killall udhcpc
292 killall wpa_supplicant 2>/dev/null
294 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
295 echo 'Shutting down Wi-Fi card'
296 iwconfig $WIFI_INTERFACE txpower off
297 fi
298 }
301 start() {
302 # stopping only unspecified interfaces
303 interfaces="$(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d')"
304 case $WIFI in
305 # don't stop Wi-Fi Interface if Wi-Fi selected
306 yes) interfaces="$(echo "$interfaces" | sed -e "/^$WIFI_INTERFACE$/d")";;
307 esac
308 for iface in $interfaces; do
309 ifconfig $iface down
310 done
312 eth; wifi
313 dhcp; static_ip
314 reconnect_wifi_network
316 # change default LXPanel panel iface
317 if [ -f /etc/lxpanel/default/panels/panel ]; then
318 sed -i "s/iface=.*/iface=$INTERFACE/" /etc/lxpanel/default/panels/panel
319 fi
320 }
323 # Looking for arguments:
325 case "$1" in
326 '')
327 boot; start ;;
328 start)
329 start ;;
330 stop)
331 stop ;;
332 restart)
333 stop; sleep 2; start ;;
334 *)
335 cat <<EOT
337 $(boldify 'Usage:') /etc/init.d/$(basename $0) [start|stop|restart]
339 Default configuration file is $(boldify '/etc/network.conf')
340 You can specify another configuration file in the second argument:
341 /etc/init.d/$(basename $0) [start|stop|restart] file.conf
343 EOT
344 ;;
345 esac