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