slitaz-boot-scripts view etc/init.d/network.sh @ rev 335
Added tag 5.3.2 for changeset 5195a3bc63bf
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Oct 30 18:28:16 2013 +0100 (2013-10-30) |
parents | b53e5c0a9026 |
children | f2a23e182b9b |
line source
1 #!/bin/sh
2 #
3 # /etc/init.d/network.sh : Network initialization boot script
4 # Configuration file : /etc/network.conf
5 #
6 . /etc/init.d/rc.functions
8 if [ -z "$2" ]; then
9 . /etc/network.conf
10 else
11 . $2
12 fi
14 boot() {
15 # Set hostname.
16 echo -n "Setting hostname to: $(cat /etc/hostname)"
17 /bin/hostname -F /etc/hostname
18 status
20 # Configure loopback interface.
21 echo -n "Configuring loopback..."
22 /sbin/ifconfig lo 127.0.0.1 up
23 /sbin/route add 127.0.0.1 lo
24 status
26 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
27 }
29 # Use ethernet
30 eth() {
31 ifconfig $INTERFACE up
32 }
34 # For wifi. Users just have to enable it through yes and usually
35 # essid any will work and the interface is autodetected.
36 wifi() {
37 if [ "$WIFI" = "yes" ] || fgrep -q "wifi" /proc/cmdline; then
38 ifconfig $INTERFACE down
40 # Confirm if $WIFI_INTERFACE is the wifi interface
41 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
42 echo "$WIFI_INTERFACE is not a wifi interface, changing it."
43 WIFI_INTERFACE=$(fgrep : /proc/net/dev | cut -d: -f1 | \
44 while read dev; do iwconfig $dev 2>&1 | \
45 fgrep -iq "essid" && { echo $dev ; break; }; \
46 done)
47 [ -n "$WIFI_INTERFACE" ] && sed -i \
48 "s/^WIFI_INTERFACE=.*/WIFI_INTERFACE=\"$WIFI_INTERFACE\"/" \
49 /etc/network.conf
50 fi
52 echo -n "Configuring $WIFI_INTERFACE..."
53 ifconfig $WIFI_INTERFACE up 2>/dev/null
54 if iwconfig $WIFI_INTERFACE | fgrep -q "Tx-Power"; then
55 iwconfig $WIFI_INTERFACE txpower on
56 fi
57 status
59 [ -n "$WPA_DRIVER" ] || WPA_DRIVER="wext"
60 IWCONFIG_ARGS=""
61 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
62 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
63 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
64 # unencrypted network
65 if [ "$WIFI_KEY" == "" -o "$WIFI_KEY_TYPE" == "none" ]; then
66 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
67 fi
68 # encrypted network
69 [ -n "$WIFI_KEY" ] && case "$WIFI_KEY_TYPE" in
70 wep|WEP)
71 #
72 # NOTE (20120303) struggled to connect with WEP key in
73 # cooking but work with 3.0. Busybox/iwconfig seems buggy
74 # but connection work with wpa_supplicant and unquoted
75 # wep_key0
76 #
77 #IWCONFIG_ARGS="$IWCONFIG_ARGS key $WIFI_KEY"
78 #iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
79 #
80 # wpa_supplicant can also deal with wep encryption
81 # Tip: Use unquoted strings for hexadecimal key in wep_key0
82 cat /etc/wpa_supplicant.conf > /tmp/wpa.conf
83 cat >> /tmp/wpa.conf <<EOF
84 ctrl_interface=/var/run/wpa_supplicant
85 ctrl_interface_group=0
86 ap_scan=1
87 network={
88 ssid="$WIFI_ESSID"
89 scan_ssid=1
90 key_mgmt=NONE
91 wep_key0=$WIFI_KEY
92 wep_tx_keyidx=0
93 priority=5
94 }
95 EOF
96 echo "Starting wpa_supplicant for NONE/WEP..."
97 wpa_supplicant -B -W -c/tmp/wpa.conf -D$WPA_DRIVER \
98 -i$WIFI_INTERFACE ;;
99 wpa|WPA)
100 # load pre-configured multiple profiles
101 cat /etc/wpa_supplicant.conf > /tmp/wpa.conf
102 cat >> /tmp/wpa.conf <<EOF
103 ctrl_interface=/var/run/wpa_supplicant
104 ctrl_interface_group=0
105 ap_scan=1
106 network={
107 ssid="$WIFI_ESSID"
108 scan_ssid=1
109 proto=WPA RSN
110 key_mgmt=WPA-PSK WPA-EAP
111 psk="$WIFI_KEY"
112 priority=5
113 }
114 EOF
115 echo "Starting wpa_supplicant for WPA-PSK..."
116 wpa_supplicant -B -W -c/tmp/wpa.conf \
117 -D$WPA_DRIVER -i$WIFI_INTERFACE ;;
118 any|ANY) cat /etc/wpa_supplicant.conf > /tmp/wpa.conf
119 cat >> /tmp/wpa.conf <<EOF
120 ctrl_interface=/var/run/wpa_supplicant
121 ctrl_interface_group=0
122 ap_scan=1
123 network={
124 ssid="$WIFI_ESSID"
125 scan_ssid=1
126 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
127 group=CCMP TKIP WEP104 WEP40
128 pairwise=CCMP TKIP
129 psk="$WIFI_KEY"
130 priority=5
131 }
132 EOF
133 echo "Starting wpa_supplicant for any key type..."
134 wpa_supplicant -B -W -c/tmp/wpa.conf \
135 -D$WPA_DRIVER -i$WIFI_INTERFACE ;;
136 esac
137 rm -f /tmp/wpa.conf
138 INTERFACE=$WIFI_INTERFACE
139 fi
140 }
142 wpa()
143 {
144 DHCP_SCRIPT="/etc/init.d/wpa_action.sh"
145 wpa_cli -a$DHCP_SCRIPT -B
146 }
148 # For a dynamic IP with DHCP.
149 dhcp() {
150 if [ "$DHCP" = "yes" ] ; then
151 echo "Starting udhcpc client on: $INTERFACE..."
152 # Is wpa wireless && wpa_ctrl_open interface up ?
153 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" = "yes" ]; then
154 wpa
155 else # fallback on udhcpc: wep, eth
156 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
157 /var/run/udhcpc.$INTERFACE.pid
158 fi
159 fi
160 }
162 # For a static IP.
163 static_ip() {
164 if [ "$STATIC" = "yes" ] ; then
165 echo "Configuring static IP on $INTERFACE: $IP..."
166 if [ -n "$BROADCAST" ]; then
167 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
168 else
169 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
170 fi
172 # Use ip to set gateways if iproute.conf exist
173 if [ -f /etc/iproute.conf ]; then
174 while read line
175 do
176 ip route add $line
177 done < /etc/iproute.conf
178 else
179 /sbin/route add default gateway $GATEWAY
180 fi
182 # wpa_supplicant waits for wpa_cli
183 [ -d /var/run/wpa_supplicant ] && wpa_cli -B
184 # Multi-DNS server in $DNS_SERVER.
185 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
186 for NS in $DNS_SERVER
187 do
188 echo "nameserver $NS" >> /etc/resolv.conf
189 done
190 if [ ! -z $DOMAIN ];then
191 echo "search $DOMAIN" >> /etc/resolv.conf
192 fi
193 for HELPER in /etc/ipup.d/*; do
194 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
195 done
196 fi
197 }
199 # Stopping everything
200 Stop() {
201 echo "Stopping all interfaces"
202 ifconfig $INTERFACE down
203 ifconfig $WIFI_INTERFACE down
205 echo "Killing all daemons"
206 killall udhcpc
207 killall wpa_supplicant 2>/dev/null
209 if iwconfig $WIFI_INTERFACE | fgrep -q "Tx-Power"; then
210 echo "Shutting down wifi card"
211 iwconfig $WIFI_INTERFACE txpower off
212 fi
213 }
215 Start() {
216 eth
217 wifi
218 dhcp
219 static_ip
220 # change default lxpanel panel iface
221 [ -f /etc/lxpanel/default/panels/panel ] \
222 && sed -i "s/iface=.*/iface=$INTERFACE/" \
223 /etc/lxpanel/default/panels/panel
224 }
226 # looking for arguments:
227 if [ -z "$1" ]; then
228 boot
229 Start
230 else
231 case $1 in
232 start)
233 Start ;;
234 stop)
235 Stop ;;
236 restart)
237 Stop
238 Start ;;
239 *)
240 echo ""
241 echo -e "\033[1mUsage:\033[0m /etc/init.d/`basename $0` [start|stop|restart]"
242 echo ""
243 echo -e " Default configuration file is \033[1m/etc/network.conf\033[0m"
244 echo -e " You can specify another configuration file in the second argument:"
245 echo -e " \033[1mUsage:\033[0m /etc/init.d/`basename $0` [start|stop|restart] file.conf"
246 echo "" ;;
247 esac
248 fi