4b1b51ecee454591a2952220f813f3ee07ce2349
[grml-network.git] / sbin / grml-ap
1 #!/bin/sh
2 # Filename:      grml-ap
3 # Purpose:       set up access point on your box
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Mit Okt 11 23:00:43 CEST 2006 [moemoe]
8 ################################################################################
9
10 # exit on any error
11 set -e
12
13 CONFIG_FILE=/etc/grml/routersetup
14 . /etc/grml/lsb-functions
15 . /etc/grml/net-functions
16 . /etc/grml/script-functions
17
18 check4root
19
20 if ! [ -r "$CONFIG_FILE" ] ; then
21         eerror "$CONFIG_FILE could not be read."
22         exit 1
23 fi
24
25 . "$CONFIG_FILE"
26
27 [ -n "$AP_ESSID" ] || AP_ESSID=grml-ap
28 [ -n "$AP_ENC" ]   || AP_ENC=off
29
30 info_message() {
31         DEV="$1"
32         echo $1 | grep -q wlan && DEV=$(echo $1 | sed 's/wlan/wifi/')
33         einfo "Finished setting up access point. Make sure your device $DEV is configured:"
34         einfo "For example put the following into /etc/network/interfaces and run \"ifup $DEV=ap\""
35         echo "
36 iface ap inet static
37       address 192.168.10.1
38       netmask 255.255.255.0
39       network 192.168.10.0
40       broadcast 192.168.10.255
41 "
42         einfo "On the client side put the following into /etc/network/interfaces and run \"ifup \$DEV=ap\""
43         echo "
44 iface ap inet static
45       address 192.168.10.2
46       netmask 255.255.255.0
47       network 192.168.10.0
48       broadcast 192.168.10.255
49       gateway 192.168.10.1
50       wireless_essid $AP_ESSID
51 "
52 }
53
54 set_ath_mode() {
55         eindent
56           einfo "Setting $1 to mode $2"
57           iwpriv $1 mode $2 ; eend $?
58         eoutdent
59 }
60
61 setupWifiDevice() {
62         DEV=$1
63         einfo "Setting wireless modes on $DEV"
64         
65           eindent
66           einfo "Setting sid to: $AP_ESSID"
67           iwconfig $DEV essid $AP_ESSID ; eend $?
68
69           [ "$AP_ENC" = off ] && ENC_INFO='off' || ENC_INFO='******'
70           einfo "Settinc encrypton to: $ENC_INFO "
71           iwconfig $DEV enc $AP_ENC ; eend $?
72
73           einfo "Setting device up"
74           ifconfig $DEV up ; eend $?
75
76         eoutdent
77 }
78
79 setup_atheros() {
80         einfo "Atheros setup: creating new WLAN AP device"
81
82         # ugly but don't know of another workaround
83         if iwconfig 2>/dev/null | grep -A1 ath0 | grep -q 'Access Point: Not-Associated' ; then
84           einfo "Destroying old ath device"
85           wlanconfig ath0 destroy ; eend $?
86         fi
87
88         device=$(wlanconfig ath create wlandev $1 wlanmode ap)
89         eend $?
90         
91         setupWifiDevice $device
92         [ -n "$ATH_MODE" ] && set_ath_mode "$device" "$ATH_MODE"
93
94         info_message $device
95         exit 0
96 }
97
98 setup_hostap() {
99         einfo "Hostap setup: creating new WLAN AP device"
100         eindent
101
102           # einfo "Changing mode of $1 to AP"
103           # ifconfig $1 down ; eend $?
104   
105           # dunno if the card is pci or pcmcia, just try to unload and
106           # load both versions,
107           [ -n $(lsmod | grep ^orinoco_pci) ] && HAPT="pci"
108           [ -n $(lsmod | grep ^orinoco_cs) ]  && HAPT="cs"
109           [ -n $(lsmod | grep ^orinoco_plx) ] && HAPT="plx"
110
111           einfo "Unloading old modules"
112           modprobe -r orinoco orinoco_$HAPT
113           eend $?
114
115           einfo "Loading new modules"
116           modprobe hostap_$HAPT
117           eend $? && setupWifiDevice $1
118
119         eoutdent
120
121         info_message $1
122         exit 0
123 }
124
125 setup_iwconfig() {
126         einfo "Trying to set $1 into mode master"
127         ifconfig $1 down
128         iwconfig $1 mode master
129         eend $? && setupWifiDevice $1
130         exit 0
131 }
132
133 setup_generic() {
134         einfo "Generic setup (no hostap / atheros capable device found): creating new WLAN AP device"
135         echo "TODO! iwconfig $1 mode Ad-Hoc"
136         exit 0
137 }
138
139 detect_wl_cards() {
140         # If you want to extend this with a specific funtion for a
141         # special driver, please have a look at /etc/grml/net-functions
142         for i in $AP_DEVICE $(getWlanDevices) ; do
143                 DRIVER=$(getLanDriver $i)
144                 case $DRIVER in
145                   ath_pci)
146                        echo $i | grep -q ath && i=$(echo $i | sed 's/ath/wifi/')
147                        setup_atheros $i
148                        ;;
149                   orinoco|hostap)
150                        setup_hostap $i
151                        ;;
152                   ipw2100|prism54)
153                        setup_iwconfig $i
154                        ;;
155                   *)
156                        setup_generic $i
157                        ;;
158                 esac
159         done
160 }
161
162 stop_devices() {
163   einfo "Searching for WLAN device with ESSID $AP_ESSID"
164   DEVICE=$(iwconfig 2>/dev/null| grep "ESSID:\"$AP_ESSID\"" | awk '{print $1}')
165   eindent
166   if [ -n "$DEVICE" ] ; then
167      for i in $DEVICE ; do
168        einfo "Found device $i" ; eend 0
169      done
170   else
171      eerror "No device(s) with ESSID $AP_ESSID found"
172      exit 1
173   fi
174   case $DEVICE in
175       ath*)
176         einfo "Shutting down $DEVICE"
177         ifdown $DEVICE
178         wlanconfig $DEVICE destroy ; eend $?
179         exit 0
180         ;;
181       *)
182         for i in $DEVICE ; do
183           einfo "Shutting down $i"
184           ifdown $i ; eend $?
185         done
186         exit 0
187         ;;
188   esac
189   eoutdent
190 }
191
192 case "$1" in
193     start)
194        detect_wl_cards
195        ;;
196     stop)
197        einfo "Trying to stop all present grml-ap setups"
198        stop_devices
199        ;;
200     restart)
201        $0 stop
202        sleep 1:
203        $0 start
204        ;;
205     *)
206     echo "Usage: $0 {start|stop|restart}"
207     exit 1
208     ;;
209 esac
210
211 eerror "Your wlan card is not supported at the moment. Sorry" ; eend 1
212 exit 1
213
214 ## END OF FILE #################################################################
215 # vim: ft=sh expandtab ai