slitaz-boot-scripts annotate etc/init.d/network.sh @ rev 198

Fix: connection to uncrypted access point (issue 257)
author Christophe Lincoln <pankso@slitaz.org>
date Sat Mar 20 10:59:48 2010 +0100 (2010-03-20)
parents 3e13b3a30ae5
children c65e4e472847
rev   line source
pankso@8 1 #!/bin/sh
MikeDSmith25@107 2 # /etc/init.d/network.sh - Network initialization boot script.
pankso@8 3 # Config file is: /etc/network.conf
pankso@8 4 #
pankso@8 5 . /etc/init.d/rc.functions
pankso@8 6
pascal@112 7 if [ -z "$2" ]; then
pascal@112 8 . /etc/network.conf
pascal@112 9 else
pascal@112 10 . $2
pascal@109 11 fi
pascal@109 12
jozee@188 13
pascal@112 14 Boot() {
pascal@112 15 # Set hostname.
paul@190 16 echo -n "Setting hostname..."
pascal@112 17 /bin/hostname -F /etc/hostname
pascal@112 18 status
pascal@112 19
pascal@112 20 # Configure loopback interface.
paul@190 21 echo -n "Configuring loopback..."
pascal@112 22 /sbin/ifconfig lo 127.0.0.1 up
pascal@112 23 /sbin/route add 127.0.0.1 lo
jozee@188 24 status
jozee@188 25 }
jozee@188 26
jozee@188 27
jozee@188 28 eth() {
paul@190 29 # Use ethernet
jozee@188 30 ifconfig $INTERFACE up
jozee@188 31 }
jozee@188 32
jozee@188 33 wifi() {
paul@190 34 # For wifi. Users just have to enable it through yes and usually
paul@190 35 # essid any will work and the interface is autodetected.
jozee@188 36 if [ "$WIFI" = "yes" ] || grep -q "wifi" /proc/cmdline; then
jozee@188 37 ifconfig $INTERFACE down
jozee@188 38
jozee@188 39 # Confirm if $WIFI_INTERFACE is the wifi interface
jozee@188 40 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
jozee@188 41 echo "$WIFI_INTERFACE is not a wifi interface, changing it."
jozee@188 42 WIFI_INTERFACE=$(grep : /proc/net/dev | cut -d: -f1 | \
jozee@188 43 while read dev; do iwconfig $dev 2>&1 | \
jozee@188 44 grep -iq "essid" && { echo $dev ; break; }; \
jozee@188 45 done)
jozee@188 46 [ -n "$WIFI_INTERFACE" ] && sed -i "s/^WIFI_INTERFACE=.*/WIFI_INTERFACE=\"$WIFI_INTERFACE\"/" /etc/network.conf
jozee@188 47 fi
jozee@188 48
paul@190 49 echo -n "Configuring $WIFI_INTERFACE..."
jozee@188 50 ifconfig $WIFI_INTERFACE up
jozee@188 51 if iwconfig $WIFI_INTERFACE | grep -q "Tx-Power"; then
jozee@188 52 iwconfig $WIFI_INTERFACE txpower on
jozee@188 53 fi
jozee@188 54 status
jozee@188 55
jozee@188 56 [ -n "$WPA_DRIVER" ] || WPA_DRIVER="wext"
jozee@188 57
jozee@188 58
jozee@188 59 IWCONFIG_ARGS=""
jozee@188 60 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
jozee@188 61 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
jozee@188 62
pankso@198 63 if [ "$WIFI_KEY_TYPE" == "" ]; then
pankso@198 64 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
pankso@198 65 fi
pankso@198 66
jozee@188 67 [ -n "$WIFI_KEY" ] && case "$WIFI_KEY_TYPE" in
jozee@188 68 wep|WEP)
jozee@188 69 IWCONFIG_ARGS="$IWCONFIG_ARGS key $WIFI_KEY"
jozee@188 70 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
jozee@188 71 # wpa_supplicant can also deal with wep encryption but iwconfig is preferred
paul@190 72 # Tip: Use unquoted strings for hexadecimal key in wep_key0
jozee@188 73 # cat /etc/wpa_supplicant.conf > /tmp/wpa.conf
jozee@188 74 # cat >> /tmp/wpa.conf <<EOF
jozee@188 75 #ctrl_interface=/var/run/wpa_supplicant
jozee@188 76 #ctrl_interface_group=0
jozee@188 77 #ap_scan=1
jozee@188 78 #network={
jozee@188 79 # ssid="$WIFI_ESSID"
jozee@188 80 # scan_ssid=1
jozee@188 81 # key_mgmt=NONE
jozee@188 82 # wep_key0="$WIFI_KEY"
jozee@188 83 # wep_tx_keyidx=0
jozee@188 84 # priority=5
jozee@188 85 #}
jozee@188 86 #EOF
jozee@188 87 ;;
jozee@188 88 wpa|WPA) cat /etc/wpa_supplicant.conf > /tmp/wpa.conf # load pre-configured multiple profiles
jozee@188 89 cat >> /tmp/wpa.conf <<EOF
jozee@188 90 ctrl_interface=/var/run/wpa_supplicant
jozee@188 91 ctrl_interface_group=0
jozee@188 92 ap_scan=1
jozee@188 93 network={
jozee@188 94 ssid="$WIFI_ESSID"
jozee@188 95 scan_ssid=1
jozee@194 96 proto=WPA RSN
jozee@188 97 key_mgmt=WPA-PSK
jozee@188 98 psk="$WIFI_KEY"
jozee@188 99 priority=5
jozee@188 100 }
jozee@188 101 EOF
paul@190 102 echo "Starting wpa_supplicant for WPA-PSK..."
jozee@188 103 wpa_supplicant -B -W -c/tmp/wpa.conf -D$WPA_DRIVER -i$WIFI_INTERFACE
jozee@188 104 ;;
jozee@188 105 any|ANY) cat /etc/wpa_supplicant.conf > /tmp/wpa.conf
jozee@188 106 cat >> /tmp/wpa.conf <<EOF
jozee@188 107 ctrl_interface=/var/run/wpa_supplicant
jozee@188 108 ctrl_interface_group=0
jozee@188 109 ap_scan=1
jozee@188 110 network={
jozee@188 111 ssid="$WIFI_ESSID"
jozee@188 112 scan_ssid=1
jozee@188 113 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
jozee@188 114 group=CCMP TKIP WEP104 WEP40
jozee@188 115 pairwise=CCMP TKIP
jozee@188 116 psk="$WIFI_KEY"
jozee@188 117 priority=5
jozee@188 118 }
jozee@188 119 EOF
paul@190 120 echo "Starting wpa_supplicant for any key type..."
jozee@188 121 wpa_supplicant -B -W -c/tmp/wpa.conf -D$WPA_DRIVER -i$WIFI_INTERFACE
jozee@188 122 ;;
jozee@188 123 esac
jozee@188 124
jozee@188 125 rm -f /tmp/wpa.conf
jozee@188 126
jozee@188 127 INTERFACE=$WIFI_INTERFACE
jozee@188 128 fi
jozee@188 129
jozee@188 130 }
jozee@188 131
jozee@188 132 wpa()
jozee@188 133 {
jozee@188 134 DHCP_SCRIPT="/etc/init.d/wpa_action.sh"
jozee@188 135 wpa_cli -a$DHCP_SCRIPT -B
jozee@188 136 }
jozee@188 137
jozee@188 138 dhcp() {
jozee@188 139
jozee@188 140 # For a dynamic IP with DHCP.
jozee@188 141 if [ "$DHCP" = "yes" ] ; then
paul@190 142 echo "Starting udhcpc client on: $INTERFACE..."
jozee@188 143 if [ -d /var/run/wpa_supplicant ]; then # wpa wireless && wpa_ctrl_open interface is up
jozee@188 144 wpa
jozee@188 145 else # fallback on udhcpc: wep, eth
jozee@188 146 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p /var/run/udhcpc.$INTERFACE.pid
jozee@188 147 fi
jozee@188 148 fi
jozee@188 149
jozee@188 150 }
jozee@188 151
jozee@188 152 static_ip() {
jozee@188 153 # For a static IP.
jozee@188 154 if [ "$STATIC" = "yes" ] ; then
paul@190 155 echo "Configuring static IP on $INTERFACE: $IP..."
jozee@188 156 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
jozee@188 157 /sbin/route add default gateway $GATEWAY
jozee@194 158 # Dirty trick: wpa_supplicant waits for wpa_cli
jozee@194 159 wpa_cli -B
jozee@188 160 # Multi-DNS server in $DNS_SERVER.
jozee@188 161 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
jozee@188 162 for NS in $DNS_SERVER
jozee@188 163 do
jozee@188 164 echo "nameserver $NS" >> /etc/resolv.conf
jozee@188 165 done
jozee@188 166 fi
pascal@112 167 }
pascal@112 168
pascal@112 169 # Stopping everything
pascal@112 170 Stop() {
pascal@112 171 echo "Stopping all interfaces"
pascal@112 172 ifconfig $INTERFACE down
pascal@112 173 ifconfig $WIFI_INTERFACE down
pascal@112 174
pascal@112 175 echo "Killing all daemons"
pascal@112 176 killall udhcpc
pankso@141 177 killall wpa_supplicant 2>/dev/null
pankso@141 178
pankso@141 179 if iwconfig $WIFI_INTERFACE | grep -q "Tx-Power"; then
pankso@141 180 echo "Shutting down wifi card"
pankso@141 181 iwconfig $WIFI_INTERFACE txpower off
pankso@141 182 fi
pascal@112 183 }
pascal@112 184
pascal@112 185 Start() {
jozee@188 186 eth
jozee@188 187 wifi
jozee@188 188 dhcp
jozee@188 189 static_ip
pascal@112 190 }
pascal@112 191
pascal@112 192
jozee@188 193 # looking for arguments:
pascal@112 194 if [ -z "$1" ]; then
pascal@112 195 Boot
pascal@112 196 Start
pascal@112 197 else
pascal@112 198 case $1 in
pascal@112 199 start)
jozee@188 200 Start ;;
pascal@112 201 stop)
jozee@188 202 Stop ;;
pascal@112 203 restart)
pascal@112 204 Stop
jozee@188 205 Start ;;
pascal@112 206 *)
pascal@112 207 echo ""
pascal@112 208 echo -e "\033[1mUsage:\033[0m /etc/init.d/`basename $0` [start|stop|restart]"
pascal@112 209 echo ""
pascal@112 210 echo -e " Default configuration file is \033[1m/etc/network.conf\033[0m"
paul@190 211 echo -e " You can specify another configuration file in the second argument:"
pascal@112 212 echo -e " \033[1mUsage:\033[0m /etc/init.d/`basename $0` [start|stop|restart] file.conf"
pascal@112 213 echo ""
pascal@112 214
pascal@112 215 esac
pascal@102 216 fi