]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/ports/wireless-ap
Let hotplug handlers return a status code
[people/ms/network.git] / src / hooks / ports / wireless-ap
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 . /usr/lib/network/header-port
23
24 HOOK_SETTINGS="HOOK ADDRESS BROADCAST_SSID CHANNEL MODE PHY SSID"
25 HOOK_SETTINGS="${HOOK_SETTINGS} ENCRYPTION KEY"
26
27 ADDRESS=$(mac_generate)
28 BROADCAST_SSID=on
29 CHANNEL=1
30 ENCRYPTION=""
31 KEY=""
32 MODE="g"
33 SSID=
34
35 function hook_check() {
36 assert isset ADDRESS
37 assert ismac ADDRESS
38 assert isset BROADCAST_SSID
39 assert isbool BROADCAST_SSID
40 assert isset CHANNEL
41 assert isset MODE
42 assert isoneof MODE a b g n
43 assert isset PHY
44 assert ismac PHY
45 assert isset SSID
46
47 if isset ENCRYPTION; then
48 assert isoneof ENCRYPTION WPA WPA2 WPA/WPA2
49
50 assert isset KEY
51 assert [ ${#KEY} -ge 8 ]
52 assert [ ${#KEY} -le 63 ]
53 fi
54 }
55
56 function hook_create() {
57 while [ $# -gt 0 ]; do
58 case "${1}" in
59 --broadcast-ssid=*)
60 BROADCAST_SSID=$(cli_get_val ${1})
61 ;;
62 --channel=*)
63 CHANNEL=$(cli_get_val ${1})
64 ;;
65 --encryption=*)
66 ENCRYPTION=$(cli_get_val ${1})
67 ;;
68 --key=*)
69 KEY=$(cli_get_val ${1})
70 ;;
71 --mac=*)
72 ADDRESS=$(cli_get_val ${1})
73 ;;
74 --mode=*)
75 MODE=$(cli_get_val ${1})
76 ;;
77 --phy=*)
78 PHY=$(cli_get_val ${1})
79 ;;
80 --ssid=*)
81 SSID=$(cli_get_val ${1})
82 ;;
83 *)
84 warning "Ignoring unknown argument '${1}'"
85 ;;
86 esac
87 shift
88 done
89
90 # Save address of phy do identify it again
91 PHY=$(phy_get ${PHY})
92 PHY=$(phy_get_address ${PHY})
93
94 local port=$(port_find_free ${PORT_PATTERN_ACCESSPOINT})
95 assert isset port
96
97 port_settings_write "${port}" ${HOOK_SETTINGS}
98
99 exit ${EXIT_OK}
100 }
101
102 function hook_edit() {
103 local port=${1}
104 shift
105
106 assert isset port
107
108 port_settings_read "${port}" ${HOOK_SETTINGS}
109
110 while [ $# -gt 0 ]; do
111 case "${1}" in
112 --broadcast-ssid=*)
113 BROADCAST_SSID=$(cli_get_val ${1})
114 ;;
115 --channel=*)
116 CHANNEL=$(cli_get_val ${1})
117 ;;
118 --encryption=*)
119 ENCRYPTION=$(cli_get_val ${1})
120 ;;
121 --key=*)
122 KEY=$(cli_get_val ${1})
123 ;;
124 --ssid=*)
125 SSID=$(cli_get_val ${1})
126 ;;
127 --mode=*)
128 MODE=$(cli_get_val ${1})
129 ;;
130 *)
131 warning "Unknown argument '${1}'"
132 ;;
133 esac
134 shift
135 done
136
137 port_settings_write "${port}" ${HOOK_SETTINGS}
138
139 exit ${EXIT_OK}
140 }
141
142 function hook_up() {
143 local port=${1}
144 assert isset port
145
146 port_settings_read "${port}" ${HOOK_SETTINGS}
147
148 # Check if the PHY is present.
149 local phy=$(phy_get ${PHY})
150 if ! isset phy; then
151 log DEBUG "phy '${PHY}' is not present"
152 exit ${EXIT_ERROR}
153 fi
154
155 # Create the wireless device, if it does not exist, yet.
156 if ! device_exists ${port}; then
157 wireless_create ${port} --phy="${phy}" --type="ap" \
158 --address="${ADDRESS}"
159 fi
160
161 # Start the hostapd service.
162 hostapd_start ${port}
163 local ret=$?
164
165 if [ ${ret} -ne ${EXIT_OK} ]; then
166 log ERROR "Could not start hostapd on port '${port}': ${ret}"
167 exit ${EXIT_ERROR}
168 fi
169
170 exit ${EXIT_OK}
171 }
172
173 function hook_down() {
174 local port=${1}
175 assert isset port
176
177 # Stop the hostapd daemon.
178 hostapd_stop ${port}
179
180 # Remove the device if it is still present.
181 if device_exists ${port}; then
182 wireless_remove ${port}
183 fi
184
185 exit ${EXIT_OK}
186 }
187
188 function hook_hotplug() {
189 local port=${1}
190 assert isset port
191
192 local phy=${2}
193 assert isset phy
194
195 assert port_exists ${port}
196
197 # Read configuration of port.
198 port_settings_read "${port}" ${HOOK_SETTINGS}
199
200 # Get the address of the phy.
201 local phy_address=$(phy_get_address ${phy})
202
203 # Check if the phy is the same we have
204 # read from the configuration file.
205 if [ "${PHY}" = "${phy_address}" ]; then
206 wireless_create ${port} --phy="${phy_address}" --type="ap" \
207 --address="${ADDRESS}"
208
209 exit ${EXIT_OK}
210 fi
211
212 exit ${EXIT_NOT_HANDLED}
213 }