]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/wireless-ap
Fix hook settings writing and checking
[people/stevee/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_settings() {
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_new() {
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_create() {
143 local port="${1}"
144 assert isset port
145
146 device_exists "${port}" && exit ${EXIT_OK}
147
148 port_settings_read "${port}" ${HOOK_SETTINGS}
149
150 # Check if the PHY is present.
151 local phy=$(phy_get ${PHY})
152 if ! isset phy; then
153 log DEBUG "phy '${PHY}' is not present"
154 exit ${EXIT_ERROR}
155 fi
156
157 # Create the wireless device
158 wireless_create "${port}" \
159 --phy="${phy}" \
160 --type="ap" \
161 --address="${ADDRESS}"
162
163 exit ${EXIT_OK}
164 }
165
166 function hook_remove() {
167 local port="${1}"
168 assert isset port
169
170 # Remove the device if present
171 if device_exists "${port}"; then
172 wireless_remove "${port}"
173 fi
174
175 exit ${EXIT_OK}
176 }
177
178 function hook_up() {
179 local port="${1}"
180 assert isset port
181
182 # The port must already exist before
183 # hostapd is started. Otherwise it will
184 # fail horribly over and over again.
185 assert device_exists "${port}"
186
187 hostapd_start "${port}"
188 }
189
190 function hook_down() {
191 local port="${1}"
192 assert isset port
193
194 hostapd_stop "${port}"
195 }
196
197 function hook_hotplug() {
198 local port="${1}"
199 assert isset port
200
201 case "$(hotplug_action)" in
202 add)
203 # Create the port when the phy is plugged in
204 if hotplug_event_port_uses_phy "${port}"; then
205 hook_create "${port}"
206 fi
207 ;;
208
209 remove)
210 # Stop hostapd
211 if hotplug_event_port_is_interface "${port}"; then
212 hostapd_stop "${port}"
213
214 exit ${EXIT_OK}
215 fi
216 ;;
217 esac
218
219 exit ${EXIT_NOT_HANDLED}
220 }