Typo
[linux_from_scratch_hints.git] / OLD / wpa-dhcpcd.txt
blob209850ad209cb3712b4c4cf5a068e12858ab34a0
1 AUTHOR: Eloi Primaux eloi@bliscat.org
2 DATE: 2006-02-19
3 LICENSE: GNU Free Documentation License Version 2
4 SYNOPSIS: Setting a wireless network with WPA and dhcpcd
5 DESCRIPTION:
6 This hint purpose a way to set wireless networks using wpa_supplicant and dhcpcd
8 ATTACHMENTS:
9 no attachments yet
11 PREREQUISITES:
12 A full LFS-6.1 and BLFS-6.1 system already configured with a standart lan network
13 The chipset driver of your wireless card (ex: madwifi http://madwifi.org/wiki/)
14 wireless tools available at http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html
15 wpa_supplicant available at http://hostap.epitest.fi/wpa_supplicant/ (latest stable release)
16 dhcpcd installed from the blfs book
19 note you will also require and access point which use wpa encryption and dhcp service
21 HINT:
23 this hint is mainly based on what has already been done in the blfs book for the dhcpcd service
25 install chipset driver, wireless tools, and wpa_supplicant
26 both of them have fully explanaited documentations to build them
27 If you still in trouble, have a look to http://madwifi.org/wiki
29 Now you need to create 2 files,
30 1)      /etc/sysconfig/network-devices/service/wpa-dhcpcd
31 2)      /etc/sysconfig/network-devices/ifconfig.XX/wpa-dhcpcd
33 XX is  the name of your device, eth0,wlan0,ath0...
35 Here are those files
37 ####### 1) wpa-dhcpcd service script    ########
38 #       
39 #  /etc/sysconfig/network-devices/service/wpa-dhcpcd
41 ####### start here ######
43 #!/bin/sh
44 # Begin $network-devices/services/wpa-dhcpcd
46 # Based on dhcpcd script adapted for  wpa networks
47 # Rewritten by Eloi Primaux  - eloi@bliscat.org
50 . /etc/sysconfig/rc
51 . $rc_functions
52 . $IFCONFIG
54 DHCPCD_PIDFILE="/var/run/dhcpcd-$1.pid"
55 DHCPCD_LEASEINFO="/var/lib/dhcpc/dhcpcd-$1.info"
56 WPA_PIDFILE="/var/run/wpa_supplicant-$1.pid"
57 #WPA_CONFIGFILE="/etc/sysconfig/network-devices/ifconfig.$1/wpa-dhcpcd"
58 #. $WPA_CONFIGFILE
59 WPA_SCRP="/etc/sysconfig/network-devices/wpa-script.$1"
60 rm -f $WPA_SCRP
61 case "$2" in
62         up)
63         RET=$?
64         evaluate_retval
65         echo "" # due to the pre ifconfig process
66         # Start wpa_supplicant in the background
67         wpa_supplicant -g/var/run/wpa_supplicant-$1-global -P$WPA_PIDFILE -B
68         #We must check if our requested SSID is available...
69         boot_mesg "Scanning for available Access Points..."
70         iwlist ath0 scan | grep "ESSID"
71         SSID_AVAILABLE=$(iwlist ath0 scan | grep "$WPA_SSID")
72         if [ -n "$SSID_AVAILABLE" ]; then
73                 boot_mesg  "The Access Point "$WPA_SSID" is  available, connecting"
74                 echo_ok
75                 # Add a new interface (no configuration file and
76                 # enable control interface)
77                 echo "wpa_cli -g/var/run/wpa_supplicant-$1-global interface_add $1 \"\" $WPA_DRIVER /var/run/wpa_supplicant" > $WPA_SCRP
78                 # Configure a network using the newly added network interface:
79                 echo "wpa_cli -i$1 add_network" >> $WPA_SCRP
80                 echo "wpa_cli -i$1 set_network 0 ssid '\"$WPA_SSID\"'" >> $WPA_SCRP
81                 echo "wpa_cli -i$1 set_network 0 key_mgmt WPA-PSK" >> $WPA_SCRP
82                 echo "wpa_cli -i$1 set_network 0 psk '\"$WPA_PASS\"'" >> $WPA_SCRP
83                 echo "wpa_cli -i$1 set_network 0 pairwise $WPA_PAIRWISE" >> $WPA_SCRP
84                 echo "wpa_cli -i$1 set_network 0 group $WPA_GROUP" >> $WPA_SCRP
85                 echo "wpa_cli -i$1 set_network 0 proto $WPA_PROTO" >> $WPA_SCRP
86                 echo "wpa_cli -i$1 enable_network 0" >> $WPA_SCRP
87                 #Now we run the little script created above
88                 boot_mesg -n "Booting wpa_supplicant on the $1 interface..."
89                 . $WPA_SCRP > /dev/null
90                 RET=$?
91                 evaluate_retval
92                 #Now we overwrite the last script by one which request the wpa status
93                 # When the network is ready it will answer "wpa_state=COMPLETED"
94                 echo "wpa_cli -i$1 status | grep 'wpa_state' | sed 's,wpa_state=,,g'" > $WPA_SCRP
95                 # we run a little loop to wait for wpa_supplicant being ready
96                 boot_mesg "Please wait while the daemon is connecting to $WPA_SSID..."
97                         RET=$(. $WPA_SCRP)
98                         COUNTER=0 # to not loop undefinetly at boot time if the network is down...
99                 while [ "$RET" != "COMPLETED" ]; do
100                         if [ "$COUTER" = "$WPA_MAXCYCLE" ]; then
101                         boot_mesg "The Access Point $WPA_SSID is available" ${WARNING}
102                         boot_mesg "But the connecting time is over . This is" ${WARNING}
103                         boot_mesg "Mostly due to a misconfigurated network." ${WARNING}
104                         boot_mesg "Please check your configuration" ${WARNING}
105                         echo_warning
106                         exit 2
107                 fi
108                         sleep 5s
109                         RET=$(. $WPA_SCRP)
110                         echo -n "$RET..."
111                 done
112                 echo ""
113                 echo_ok
114                 
115                 # At this point, the new network interface should start trying to associate
116                 # with the WPA-PSK network using SSID.
119                 boot_mesg  "Starting dhcpcd on the $1 interface..."
120                 # Test to see if there is a stale pid file
121                 if [ -f "$DHCPCD_PIDFILE" ]
122                 then
123                     ps `cat "$DHCPCD_PIDFILE"` | grep dhcpcd > /dev/null
124                     if [ $? != 0 ]
125                     then
126                         rm -f $DHCPCD_PIDFILE > /dev/null
127                     else
128                         boot_mesg "dhcpcd already running!" ${WARNING}
129                         echo_warning
130                         exit 2
131                     fi
132                 fi
133                 /sbin/dhcpcd $1 $DHCP_START
134                 # Save the return value
135                 RET="$?"
136                 # Print the assigned settings if requested
137                 if [ "$RET" = "0" -a "$PRINTIP" = "yes" ]; then
138                         . /var/lib/dhcpc/dhcpcd-$1.info
139                         if [ "$PRINTALL" = "yes" ]; then
140                                 echo ""
141                                 echo_ok
142                                 boot_mesg "           DHCP Assigned Settings for $1:"
143                                 boot_mesg_flush
144                                 boot_mesg "           IP Address:      $IPADDR"
145                                 boot_mesg_flush
146                                 boot_mesg "           Subnet Mask:     $NETMASK"
147                                 boot_mesg_flush
148                                 boot_mesg "           Default Gateway: $GATEWAY"
149                                 boot_mesg_flush
150                                 boot_mesg "           DNS Server:      $DNS"
151                                 boot_mesg_flush
152                         else
153                                 boot_mesg " IP Addresss: ""$IPADDR"
154                                 echo_ok
155                         fi
156                 else
157                         echo ""
158                         $(exit "$RET")
159                         evaluate_retval
160                 fi
161         else
162                 boot_mesg "The Access Point "$WPA_SSID" is not available" ${WARNING}
163                 boot_mesg "Please check your configuration" ${WARNING}
164                         echo_warning
165                         exit 2
166         fi
168         ;;
170         down)
171                 boot_mesg -n "Stopping dhcpcd on the $1 interface..."
172                 # Do nothing with the client daemon if we have an infinate 
173                 # lease time as the client exits when started in this case,
174                 # just echo OK.
175                 if [ -e $DHCPCD_LEASEINFO ]
176                 then
177                     . $DHCPCD_LEASEINFO
179                     if [ "$DHCPCD_LEASETIME" = "4294967295" ]
180                     then
181                         # do nothing, just echo ok
182                         echo ""
183                         echo_ok
184                     else
185                         if [ -n "$DHCP_STOP" ]
186                         then
187                             /sbin/dhcpcd $1 $DHCP_STOP &> /dev/null
188                             RET="$?"
189                             if [ "$RET" -eq 0 ]; then
190                                 echo ""
191                                 echo_ok
192                             elif [ "$RET" -eq 1 ]; then
193                                 boot_mesg "dhcpcd not running!" ${WARNING}
194                                 echo_warning
195                             else
196                                 echo ""
197                                 echo_failure
198                             fi
199                         else
200                             echo ""
201                             killproc dhcpcd
202                         fi
203                     fi
204                 else
205                     boot_mesg -n "LEASEINFO Test failed! - " ${WARNING}
206                     boot_mesg "dhcpcd is not running!" ${WARNING}
207                     echo_warning
208                     exit 1
209                 fi
210                 boot_mesg "Stopping wpa_supplicant on the $1 interface..."
211                 killproc -p $WPA_PIDFILE wpa_supplicant
214         ;;
216         *)
217                 echo "Usage: $0 [interface] {up|down}"
218                 exit 1
219         ;;
220 esac
222 # End $network_devices/services/wpa-dhcpcd
225 #### end here####
230 ####### 2) wpa-dhcpcd config file       ########
232 # /etc/sysconfig/network-devices/ifconfig.XX/wpa-dhcpcd
233 # XX is  the name of your device, eth0,wlan0,ath0...
235 #### start here #####
237 #This config file is mostly based of the
238 #dhcpcd config file
240 ONBOOT="yes"
241 SERVICE="wpa-dhcpcd"
242 DHCP_START="-d " # to print some debug informations
243 DHCP_STOP="-k "
244 WPA_SSID="Your SSID"  
245 WPA_DRIVER="Your Driver"
246 WPA_PROTO="Your Proto" # WPA2 for WPA2-PSK check the wpa-supplicant readme
247 WPA_PAIRWISE="Your Pairwise" # CCMP for AES
248 WPA_GROUP="Your Group" # CCMP for AES
249 WPA_PASS="Your very secret WPA password"
251 #one cycle each 5 seconds
252 WPA_MAXCYCLE=24 # 120s
253 # Set PRINTIP="yes" to have the script print
254 # the DHCP assigned IP address
255 PRINTIP="no"
257 # Set PRINTALL="yes" to print the DHCP assigned values for
258 # IP, SM, DG, and 1st NS. This requires PRINTIP="yes".
259 PRINTALL="yes"
262 ### End Here ##### 
265 do a chmod 755 /etc/sysconfig/network-devices/service/wpa-dhcpcd
266 and if you correctly set the config file, just type
267 /etc/rc.d/init.d/network restart if you can use the connection then
268 your wireless network will be set at boot time
270 ACKNOWLEDGEMENTS:
273 CHANGELOG:
275 -- 
276 http://linuxfromscratch.org/mailman/listinfo/hints
277 FAQ: http://www.linuxfromscratch.org/faq/
278 Unsubscribe: See the above information page